Issue
I am trying to retrieve my data from Parse.com during splash screen.
I'm making the query in the DoInBackground method and add all objects retrieved in an object vector (which is in an other class).
when moving to the MainActivty all data get loss.
Here's my code:
private class loadDataTask extends AsyncTask<Void, Void, Vector<PartyObj>>{
@Override
protected Vector<PartyObj> doInBackground(Void... params)
{
ParseQuery query = new ParseQuery("partyObj");
query.whereExists("Name");
query.findInBackground(new FindCallback() {
@Override
public void done(List<ParseObject> mNameList, ParseException e) {
if (e == null) {
adapter.partyVector = new Vector<PartyObj>();
for(int i=0; i<mNameList.size(); i++){
PartyObject party = new PartyObject();
party.setIndex(i);
party.setmName(mNameList.get(i).getString("Name").toString());
adapter.partyVector.add(party);
}
}else {
Log.d("mNameList", "Error: " + e.getMessage());
}
}
});
return adpater.partyVector;
}
@Override
protected void onPostExecute(Vector<PartyObj> result) {
progressDialog.dismiss();
setContentView(R.layout.activity_main_menu);
}
}
Solution
It seems like you're using the AsyncTask all wrong. query.findInBackground() is as far as I can tell an asynchronous call so you're essentially wrapping one async call in another. The problem with this is that code execution continues immediately after query.findInBackground(), thus ending the doInBackground() method. Since you move to the main activity immediately when the async task is finished it may be that query.findInBackground() hasn't finished yet (thus resulting in an empty/partial list).
Are you really sure you need to wrap everything in an async call? Does either of
ParseQuery query = new ParseQuery("partyObj");
query.whereExists("Name");
take time to execute or is the bulk of the work already in the async findInBackground() call? If so, I'd say skip the AsyncTask and do the view transition in the done() method of the FindCallback.
UPDATE: I've read up on ParseQuery and it clearly says:
Using the callback methods is usually preferred because the network operation will not block the calling thread. However, in some cases it may be easier to use the find, get or count calls, which do block the calling thread. For example, if your application has already spawned a background task to perform work, that background task could use the blocking calls and avoid the code complexity of callbacks.
And in findInBackground() it clearly states that:
Retrieves a list of ParseObjects that satisfy this query from the server in a background thread. This is preferable to using find(), unless your code is already running in a background thread.
Since you are already wrapping everything in an AsyncTask you should either skip the async task completely since findInBackground( is already running in a background thread or switch to the blocking find() method.
Example without AsyncTask but using async findInBackground():
ParseQuery query = new ParseQuery("partyObj");
query.whereExists("Name");
query.findInBackground(new FindCallback() {
@Override
public void done(List<ParseObject> mNameList, ParseException e) {
if (e == null) {
adapter.partyVector = new Vector<PartyObj>();
for(int i=0; i<mNameList.size(); i++){
PartyObject party = new PartyObject();
party.setIndex(i);
party.setmName(mNameList.get(i).getString("Name").toString());
adapter.partyVector.add(party);
}
progressDialog.dismiss();
setContentView(R.layout.activity_main_menu);
}
else {
Log.d("mNameList", "Error: " + e.getMessage());
}
}
});
Example with AsyncTask and blocking find()
private class loadDataTask extends AsyncTask<Void, Void, Vector<PartyObj>>{
@Override
protected Vector<PartyObj> doInBackground(Void... params)
{
ParseQuery query = new ParseQuery("partyObj");
query.whereExists("Name");
Vector<PartyObject> v = new Vector<PartyObject();
try {
List<ParseObject> queryResult = query.find();
for(ParseObject po : queryResult) {
PartyObject party = new PartyObject();
party.setIndex(partyVector.size());
party.setmName(po.getString("Name").toString());
v.add(party);
}
}
catch(ParseException e) {
Log.d("mNameList", "Error: " + e.getMessage());
}
return v;
}
@Override
protected void onPostExecute(Vector<PartyObj> result) {
adapter.partyVector.addAll(result);
progressDialog.dismiss();
setContentView(R.layout.activity_main_menu);
}
}
Answered By - britzl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.