0

Async Json feed finishing later than AyncTasks to load data from sqllite DB. How to sync them or any better solution ?

I searched StackOverFlow but could not find this peculiar problems like mine.

In my app, All data is shown to the user from sqllite DB, which is present in mobile itself. Data in sqlite DB is created using AsyncTask JSON feed on click of buttons. In one case of mine, AsyncTask to download data is kicked off in background but user had already landed in list fragment to see data. To ensure that user is not waiting for download to finished, i implemented this also in Async task. Now, this Async task to show data finished much before Async data to download and insert. This means that user always see old data and have to come back to see new data. Any solution for this peculiar problem would be appreciated. Thanks.

Sachiin Gupta
  • 378
  • 2
  • 9
  • If the feed consists of several objects, you could seperate the task into several smaller tasks, and then display them one by one, as they load? – Malte R Oct 19 '16 at 15:32
  • Feed gives back only one object and List shows the same to the user (but from a database not directly from feed). – Sachiin Gupta Oct 19 '16 at 15:33
  • Listen for changes in the db table from activity, and re-QUERY whenever a change is detected. So the network call will just save to db table thus triggering the re-QUERY. – Elvis Chweya Oct 19 '16 at 15:37
  • Ok, then your solution seems quite fitting, though you could use an event to signal that the download has finished, and refresh the content without the user having to leave the page and come back – Malte R Oct 19 '16 at 15:37
  • Or even use an evenbus that sends feed data to UI after saving to db from network call. – Elvis Chweya Oct 19 '16 at 15:38

1 Answers1

0

I am able to fix this by implementing Handler as talking mechanism between two Async tasks. I took help of this post. Once a DB inserts are over in Async Task then I am sending message to the called program to reset the Adapter of List. Thanks to everyone for help in this. https://stackoverflow.com/a/26139605/5845259

Community
  • 1
  • 1
Sachiin Gupta
  • 378
  • 2
  • 9