4

I'm using TouchDB to replicate a DB and display its contents in a series of ListViews in a ViewPager. The problem I'm having is that on the first replication TouchDB calls it's onSuccess()/onPostExecute() methods before it's finished replicating, meaning I can't call invalidate() on my ViewPager to get it to draw the ListViews.

Is there any solution to this?

KingFu
  • 1,358
  • 5
  • 22
  • 42

1 Answers1

3

You can add a runnable that will invalidate to the end of your run queue.

myView.post(new Runnable() {
  @Override
  public void run() {
    myView.invalidate();
  }
});

Does this work?

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • The only way to really solve your problem might be to define a listener interface that you instantiate and pass to your DB code. When the DB code has actually finished, it will call back into the listener. In the implementation of that listener instance, you can call invalidate on your view. – Sky Kelsey Apr 04 '13 at 19:53
  • ah thanks, I thought your Runnable code solved it but seems the DB threads were still finishing last. I see what you mean about the listener interface but not sure I'm going to be to manage that as I don't know enough about what's going on under the hood – KingFu Apr 04 '13 at 20:06
  • Used a variation of your code and got it to run invalidate after the db threads. seems it's not a problem with the viewpager but something to do with the viewquerys of the db I think – KingFu Apr 04 '13 at 20:16
  • The DB Thread finishes last because it is long running. Using post() queues up threads to run, so that they run sequentially. But they also run on the UI, thread, so you wouldn't want to have a long running task such as a DB query execute that way. – Sky Kelsey Apr 04 '13 at 20:55