Android AsyncTask - one subclass for working with a database?

I have activity to initialize a game that performs multiple fetch and paste from multiple SQLite tables.

I am trying to understand AsyncTask, but from all the examples that I have read so far, I am wondering if I need to subclass AsyncTask for every single data operation that I need to do?

For example, my activity in NewGame does the following:

1) Insert new player record into PLAYER table 2) Insert new player pet record into PET table 3) Select cursor of n records from INVENTORY 4) Insert array of ranomly chosen inventory items into PLAYER_OWNED table 5) ....more things of a similar nature 

There will be several more selections and inserts for different things, so having a separate subclass for each of them will be crazy. Not to mention that in this game there will be about 8 events, which largely depend on reading and writing the database.

So basically, what is the best way to use AsyncTask to perform several different SQLite operations?

+4
source share
1 answer

You can pass parameters to AsyncTask, even if you use nested clans, you can use global variables from the AsyncTask class using one of the above or both mentioned auxiliary tools, you should be able to use the same class and let it do different things in depending on the parameter you are passing. I see no real need to define multiple AsyncTasks.

You will need to define AsyncTask in each action.

I wrote the information you need, because you really do not need it, but it is convenient to do so, and it is easy to read / write code, since AsyncTask is associated only with this activity. This, of course, means that you use nested clans, I see no reason to write a separate class file for AsyncTask only.

+3
source

All Articles