Search code examples
androidandroid-asynctaskintentserviceandroid-syncadapter

Syncing mehod for android chat application


I am implementing a chat app in android. A vital part of this app is to sync with the server and local database. There are several methods to sync data between server and android device like AsyncTask, IntentService and SyncAdapter.

I prefer to use SyncAdapter, because it is more efficient and it handles most of the background tasks by itself.

When I read the developer page for SyncAdapter I found this,

Note: Sync adapters run asynchronously, so you should use them with the expectation that they transfer data regularly and efficiently, but not instantaneously. If you need to do real-time data transfer, you should do it in an AsyncTask or an IntentService.

Does that means is it not good to use like chat app?

Also I need to mention a feature of SyncAdapter

Automated execution Allows you to automate data transfer based on a variety of criteria, including data changes, elapsed time, or time of day. In addition, the system adds transfers that are unable to run to a queue, and runs them when possible.

So if it starts to sync when data changes (Since the new messages are stored in the sqlite database), I think SyncAdapter will be a good choice for Chat App.

Any Suggestions are appreciated.

Thanks.


Solution

  • Usually mobile app depends on backend implementation and app requirements, but generally you shouldn't use such methods for chat application, they won't give you up to date data.

    I'd say when app is in background, you should use GCM for new messages notifications and when app is in foreground use something like RPC, xmpp, sockets or whatever that keeps your connection alive.