When SyncAdapter runs synchronization on android? - android

Let's say, my application implements SyncAdapter functionality and doesn't define periodic syncs. When synchronization will happen in such scenario? First scenario I may think about is local ContentProvided/database content change.
What is about server changes? How SyncAdapter will know about that?

If you have no periodic sync setup, Sync will happen if your code explicitly calls ContentResolver.requestSync(Account account, String authority, Bundle extras) with your account and authority.
Also, if your ContentProvider insert or update or delete functions call ContentResolver.notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork), if the bool syncToNetwork is true (the default), it will also trigger a sync. There's a short delay induced here, to ensure that a batch of database changes only causes one sync, not one per change. Note that your code should be calling notifyChange because it's how Android signals your UI to update after Content that the UI is reflecting had been changed.
If the server database changes, your app won't know, because sync isn't happening. Two options:
Use periodic sync. This will be cleaner if your server API implements etags or the if-modified-since http headers to filter the data you sync so only the updates come down.
C2DM (Cloud 2 Device Messaging) Essentially, push notification for Android. Requires some server components -- You tie a device ID to an account on the server and when the server changes, it has to explicitly send a message to the device to tell it to update. This is custom code work on the server to support android specifically, but once you invest the time, it's great. C2DM is how Android gets gmail to show up on your device 10 seconds after it arrives in your inbox, rather than at the next 10 minute periodic sync. It's also more battery efficient since you only turn on the radio and sync when you know there's new data to get.

Related

What solution is better for sync offline-mode app with server once a minute

I've made Android app that uses offline-mode. Also it has button "Sync", on click - syncronization with server is performed (server is not Firebase-service DB).
I want to do the same sync without this button once a minute when my app is on foreground and when network connection is on.
WorkManager seems the best solution for the usecase, but according to this article minimum interval for WorkManager is 15 minutes.
Other ways described in this article are: ForegroundService, AlarmManager and background Thread.
Also I found solution Sync Adapter
What way would be the most efficient for the case?
P.S. I understand that this scenario is not very clean and probably the best would be add online-mode and switch between two modes without frequent syncs. But I have some reasons at this time not to do that
Finally, I chose next way:
To invoke one-minute sync I used ThreadPool. This discussion helped me to choose.
Inside my Application class I put that code:
val scheduler = Executor.newSingleThreadScheduledExecutor()
scheduler.scheduleAtFixedRate({
.... <My Sync Block> ....
}, 0, 1, TimeUnit.MINUTES)
To prevent getting the same data from the server (there could be huge pieces of data in my case), I had to use MD-5 alghoritm on the server-side. It works as follows:
server emits data with hashes (for each piece od data)
mobile app gets data and saved both data and hashes in SQLite. In next sync app sends hashes back in request
server looks if requested data has different hashes and includes only updated data in respond

Firebase database backgroundservice error [duplicate]

I'm considering the use of keepSynced() for some data from Firebase Realtime Database. I understand that it will automatically sync those paths. But how does that relate to Android lifecycle? If the user leaves all activities (and all normal listeners disconnect), will it stop syncing? I don't want the app to become data or battery hog.
On the other hand, I would like to update cached data when FCM notification arrives. I can launch some service which will connect to Firebase. I would like to sync all paths which are in keepSynced() and stop it when it's synced. I'm not sure how to achieve that. Create a listener to one of the paths and keep the service running for some time? After the service is finished, will it stop syncing?
firebaser here
Great question!
When there is no active activity, the operating system may close the connection to the Firebase database at any time. Our SDKs don't try to prevent that, but will reconnect when the app becomes active again.
What you're describing in your second paragraph is what we call "push to sync", where you send a push notification (typically a silent FCM data message) to trigger synchronizing of the data.
We did something like that in last year's I/O app and, while it was a bit more complex than we wanted it to be, it worked great. We explicitly managed the connection in that case, calling goOnline() and goOffline() (after 5 minutes iirc). The main sync code can be found in the IOSched github repo.

Handling keepSynced() while on background on Android and with FCM

I'm considering the use of keepSynced() for some data from Firebase Realtime Database. I understand that it will automatically sync those paths. But how does that relate to Android lifecycle? If the user leaves all activities (and all normal listeners disconnect), will it stop syncing? I don't want the app to become data or battery hog.
On the other hand, I would like to update cached data when FCM notification arrives. I can launch some service which will connect to Firebase. I would like to sync all paths which are in keepSynced() and stop it when it's synced. I'm not sure how to achieve that. Create a listener to one of the paths and keep the service running for some time? After the service is finished, will it stop syncing?
firebaser here
Great question!
When there is no active activity, the operating system may close the connection to the Firebase database at any time. Our SDKs don't try to prevent that, but will reconnect when the app becomes active again.
What you're describing in your second paragraph is what we call "push to sync", where you send a push notification (typically a silent FCM data message) to trigger synchronizing of the data.
We did something like that in last year's I/O app and, while it was a bit more complex than we wanted it to be, it worked great. We explicitly managed the connection in that case, calling goOnline() and goOffline() (after 5 minutes iirc). The main sync code can be found in the IOSched github repo.

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.
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.

Android design- Data sync approach

I have following scenario:
Android application running on mobile device needs to have data syncing feature based on given set of interval specified by mobile application user.
I have designed a UI which will accept syncing frequency from user and persist in sqlite db. I was wondering what would be right approach for building syncing logic. Server exposes restfull webservice for getting delta changes in data. Syncing should happen even if application is in dormant( application is not active) Background syncing should be invoked on specified frequency.
Can i use make use service along alarm for implementing this?
Is there a better approach for implementing this?
Use the SyncAdapter class, a Service, AlarmManager, and every component that follows from using the SyncAdapter class.
Using a SyncAdapter allows the user to control the syncing from the same place he controls the accounts and the syncing of all his other apps: google apps, facebook, twitter, whatsapp, etc.
This is really the best way to do syncing. It's part of the standard UI the user is used to. And it's also the most energy efficient way you can do syncing.
Also, using a SyncAdapter doesn't preclude you from having the user set the frequency of the Syncs inside your app (as long as the primary way of turning on and off the syncing still goes through the "Accounts and Sync" main settings of the device itself).

Categories

Resources