9

There's a REST service that I use to populate info in my database, that is later used by my app. I've read several threads on the matter, and now have to decide how I want the sync between the REST service and my DB to work.

Think of an app that gets info from google finance APIs about stocks and stores it in a DB, displays the information when the app is launched, and sends notifications when specific events happen in the stock price.

I already implemented the simple option of AsyncTask that is launched when a user manually requests a sync. Now I have to implement the auto-sync and these are the options I found:

  • Create a Service that will do the syncing
  • Use a Sync Adapter / Sync Provider

So I found a lot of advantages to the second option, particularly those explained in this video, but also two major disadvantages:

  • I couldn't find good documentation for android sync (a few third party articles such as these, but no official Google or Android Developers guide)
  • According to this article messing up in a Sync Adapter can cause major issues such as OS crash and reboot.

Most of the info I found is pretty old, so maybe things have changed since, but my App is supposed to work with API level 8 and above, so I would be very thankful for any recommendations and links to valuable documentation.

tbkn23
  • 5,205
  • 8
  • 26
  • 46
  • Just to be clear, do you want the app to download/sync data even while the app is not in foreground? Possible solutions may vary depending on this. – Esparver May 08 '13 at 07:20
  • Yes, I do. For the notifications. – tbkn23 May 08 '13 at 08:01
  • Do you want the app send periodically request to know if there is a change ? – Pierre Criulanscy May 08 '13 at 09:57
  • I think so. The REST server is not under my control so I can't have it notify the app. So I think the only other option is for the app to contact it periodically and sync. – tbkn23 May 08 '13 at 10:35

2 Answers2

3

Definitely go with SyncAdapter. Follow instructions here: Why does ContentResolver.requestSync not trigger a sync?.

To do SyncAdapter, you'll also need to make an Account/AccountAuthenticator as well, so your SyncAdapter knows how to login to your service -- Unless it truly is the Google Finance APIs, in which case all you need to do is apply the right permissions in the manifest so that it will use the account settings already on the phone.

You didn't link the article you mentioned that discussed crashes, but I know it, and it's actually talking about Accounts -- Which yes, you do need to be careful with.

Community
  • 1
  • 1
jcwenger
  • 11,383
  • 1
  • 53
  • 65
0

Checkout https://github.com/sschendel/SyncManagerAndroid-DemoGoogleTasks.

Demonstrates setting up sync of Google Task API data to local SQLite db.

Sean
  • 7,562
  • 10
  • 27
  • 29