0

I was making two Parse.com queries ..

ABC.findInBackground(new FindCallback<ParseObject>()
 {
 ...
 }
DEF.findInBackground(new FindCallback<ParseObject>()
 {
 ...
 }

and I essentially tried doing this,

CountDownLatch waitForBoth = new CountDownLatch(2);

ABC.findInBackground(new FindCallback<ParseObject>()
 {
 ...
 waitForBoth.countDown();
 }
DEF.findInBackground(new FindCallback<ParseObject>()
 {
 ...
 waitForBoth.countDown();
 }
 // (error handling (just a flag) not shown)

waitForBoth.await();
Log("both are finished yay!");

of course, I just get the sundry errors IllegalMonitorStateException: object not locked by thread before wait() etc etc etc.

Part of the problem other than me being dense is, those calls to Parse's findInBackground in fact, take you off on a new thread anyway, right?

For the record, one of those Parse calls looks like this,

ParseQuery<ParseObject>PQ = ParseQuery.getQuery("SomeTable");

PQ.whereEqualTo("SomeColumn", ParseUser.getCurrentUser());
PQ.include("SomeOtherColumn");

PQ.findInBackground(new FindCallback<ParseObject>()
{
@Override
public void done(List<ParseObject> res, ParseException e)
  {
  if (e == null)
    {
    // res is the List of ParseObject results from the cloud
    // here, you'd basically...
    waitForBoth.countDown();
    }
  else
    {
    int errCodeSimple = e.getCode();
    exampleWoeFlag = false;
    waitForBoth.countDown();
    }
  }
});
}

and the doco on that ...

http://parse.com/docs/android/api/com/parse/ParseQuery.html#findInBackground(com.parse.FindCallback)

Fattie
  • 27,874
  • 70
  • 431
  • 719

1 Answers1

1

The Parse SDK does create a new Thread, but the "done" method is running on the UI Thread. This means you are calling "countDown" on the UI Thread, but you will never reach there, because you have called "await" on it.

You could solve it by creating your own Threads and passing the CountDownLatch in it. And then use Parse's synchronous methods

does that make sense ? Someone correct me if Im wrong

By the way, there is an excellent (free) course running on coursera at the moment (https://www.coursera.org/course/posa) that discusses the CountDownLatch in Android.

kind regards Anton