I'm trying to understand the workings of SyncAdapters. I looked at the code for when we receive a GCM notification from a broadcast receiver. We want to force the SyncAdapter to sync immediately by calling requestSync as the GCM is telling my app that there is new data available to fetch.
I got the code below from this link: https://software.intel.com/en-us/articles/handling-offline-capability-and-data-sync-in-an-android-app-part-2
public class GcmBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = GcmBroadcastReceiver.class.getSimpleName();
public GcmBroadcastReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(gcm.getMessageType(intent)) &&
intent.getExtras().containsKey("com.example.restaurant.SYNC_REQ")) {
Log.d(TAG, "GCM sync notification! Requesting DB sync for server dbversion " + intent.getStringExtra("dbversion"));
ContentResolver.requestSync(new Account("dummyaccount", "com.example.restaurant"),
RestaurantContentProvider.AUTHORITY, Bundle.EMPTY);
}
}
}
Now I read about SyncAdapters from here: http://naked-code.blogspot.com/2011/05/revenge-of-syncadapter-synchronizing.html) and they mentioned that:
a SyncAdapter is just a service that is started by the sync manager, which in turn maintains a queue of SyncAdapters.
If my phone has a couple of different SyncAdapters related to different apps (Facebook, Twitter etc.) and they all have items they need to sync (for example, new posts on Facebook or Twitter), it looks like all these SyncAdapters would be put into a queue waiting till the SyncManager gives the go-ahead to sync all these SyncAdapters.
If my app calls the command to sync now - ContentResolver.requestSync(), does that mean that all the other apps (Facebook, Twitter etc) also gets sync-ed at the same time?
I looked at the definition of requestSync method:
http://developer.android.com/reference/android/content/ContentResolver.html#requestSync(android.accounts.Account, java.lang.String, android.os.Bundle)
public static void requestSync (Account account, String authority, Bundle extras)
Start an asynchronous sync operation. If you want to monitor the progress of the sync you may register a SyncObserver.
public static void requestSync (SyncRequest request)
Register a sync with the SyncManager. These requests are built using the SyncRequest.Builder.
Is the requestSync calling the SyncManager to sync all its syncAdapters?