vendredi 27 février 2015

concurent use of two instances of AsyncTask to get data from database and callbacks



I have two fragments that instansiate concurently that both query the database using the same AsyncTask.



public Fragment getItem(int position) {
Log.d("position", String.valueOf(position));
switch (position) {
case 0:
return SupplementsWiki.newInstance();
case 1:
try {
return HealthLogger.newInstance(loginResponse.getString("uid"));
} catch (JSONException e) {
e.printStackTrace();
}
default:
return null;
}

}


Is is known that:



The FragmentPagerAdapter instantiates 2 Fragments on start, for index 0 and for index 1.



So, both fragment makes a query to the database at the same time, to fetch some data to render on the screen.


Fragment in case 1:



dbFunctions.fetchHealthData(userId, new MyCallback() {
@Override
public void onRemoteCallComplete(JSONObject jsonFromNet) throws JSONException, ExecutionException, InterruptedException {
Log.d("HEALTH DATA FROM NET", jsonFromNet.toString(1));
JSONArray health_data = jsonFromNet.getJSONArray("health_data");


And fragment in case 0: `



DBFunctions DBFunctions = new DBFunctions(getActivity());
DBFunctions.fetchAllSupplements(new MyCallback() {
@Override
public void onRemoteCallComplete(JSONObject jsonFromNet) throws JSONException, ExecutionException, InterruptedException {
JSONArray jsonArray = jsonFromNet.getJSONArray(VITAMIN_TAG);
Log.d("json_array", jsonArray.toString(1));
populateList(jsonArray);
}



Both of them use a callback interface mechanism. So, in logcat something strange happens. The callback method returns wrong data to the overriden method. It prints out the HEALTH DATA FROM NET twice instead of once and the second callback method is NEVER called. json_array tag from Log.d method is never called. So obviously something strange happens with the asynctask and the callback method.




D/HEALTH DATA FROM NET﹕ {
"tag": "fetch_health_data",
.....
...
..
}


D/HEALTH DATA FROM NET﹕ {
"tag": "populate_vitamins_list",
...
...
..
}


To execute the asynctasks i use THIS method.



@TargetApi(Build.VERSION_CODES.HONEYCOMB) // API 11
public static <T> void executeAsyncTask(AsyncTask<T, ?, ?> asyncTask, T... params) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
else
asyncTask.execute(params);
}

public void fetchHealthData(String userId, MyCallback myCallback) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", fetch_health_data_tag));
params.add(new BasicNameValuePair("user_id", userId));
// params.add(new BasicNameValuePair("password", password));
AsyncTask<Void, Void, JSONObject> db = new DbHandler(activity, myCallback, params);
executeAsyncTask(db);

}


public void fetchAllSupplements(MyCallback callback) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", fetch_vitamins_tag));
jsonParser = new DbHandler(activity, callback, params);
executeAsyncTask(jsonParser);

}



Aucun commentaire:

Enregistrer un commentaire