SyncAdapter uses only the latest advanced features for several requestSync () calls

I use SyncAdapter and GCM to notify my application of various types of changes on the backend server. For example, if change A occurs on the server, I do a push notification with the field change_a, which I pass to the SyncAdapter through ContentResolver.requestSync() . Thus, the SyncAdapter knows what needs to be synchronized with the server. Similarly, to change B, I send a field named change_b.

This works fine except for one use case. I send a change_a notification, it calls ContentResolver.requestSync() , but since there is no network availability, SyncAdapter is not called yet. If after that I send a change_b notification, ContentResolver.requestSync() is called again with a new field. And then, when the network is up, the SyncAdapter is SyncAdapter , but only with the last field change_b and, accordingly, it does not synchronize change A.

So basically ContentResolver.requestSync() cancels all previous calls to requestSync() that haven't activated SyncAdapter yet. SyncAdapter starts with the latest additions that have been sent.

One solution to this is to not distinguish between change A and change B and have the SyncAdapter synchronize everything. But it's expensive for bandwidth. I want to control what and when to synchronize. Is there something I could do to fix this (maybe a sync flag)?

+4
source share
2 answers

I seem to have made the wrong conclusions. In fact, after some additional testing, if requestSync() is called with different additions, the SyncAdapter is called several times for each individual package.

My problem was somewhere else. When I receive a push notification, I would schedule an alarm to trigger a synchronization request, and I would use a PendingIntent with the PendingIntent.FLAG_CANCEL_CURRENT flag, which means that if another push notification occurs before the alarm, it will be overwritten with new data. So, if the device was disconnected when it appeared on the network, it received all pending push notifications, and only one alarm was set with data from the most recent push notification.

As I solved this problem, it is to set different actions for each PendingIntent so that setting a new alarm of the same type does not overwrite other types of alarms.

And an improvement on this would be to add collapse_key for each type of synchronization so that only one of each type of push notification is delivered to the device.

+3
source

I seem to have made the wrong conclusions. In fact, after some additional testing, if requestSync () is called with various additions, the SyncAdapter is called several times for each individual package.

If you use different settings in multiple calls to requestSync() , then performSync() is called for each requestSync() . I want to add proof: a call toKey() inside SyncOperation.java includes the values ​​from the inline package. This prevents the SyncManager from subtracting these SyncOperations .

+1
source

Source: https://habr.com/ru/post/1445191/


All Articles