Reputation: 1035
I'm writing an Android Sync Adapter and basically having a problem with it syncing in an infinite loop. As soon as the sync completes it starts all over again.
Thank you,
Regards,
Akshay
@Override
public void onPerformSync(final Account account, final Bundle extras, final String authority, final ContentProviderClient provider, final SyncResult syncResult) {
Log.i("Sync result full sync = " + syncResult.fullSyncRequested);
Log.i("Sync result " + syncResult.toDebugString());
Log.i("Bundle " + extras.toString());
final CountDownLatch latch = new CountDownLatch(3);
final CachedDataReceiver globalStreamRefreshReciever = new CachedDataReceiver(null) {
@Override
protected void onComplete(int resultCode) {latch.countDown();}
@Override
protected void onError() {latch.countDown();}
};
final CachedDataReceiver newMessagesReciever = new CachedDataReceiver(null) {
@Override
protected void onComplete(int resultCode) {latch.countDown();}
@Override
protected void onError() {latch.countDown();}
};
final CachedDataReceiver getViewedMessagesReciever = new CachedDataReceiver(null) {
@Override
protected void onComplete(int resultCode) {latch.countDown();showAnyNewInboxItemAlerts(getApplicationContext());}
@Override
protected void onError() {latch.countDown();}
};
/*long currentTime = System.currentTimeMillis();
long netTime = currentTime-getLastSyncTimeStamp();
boolean shouldSync = (netTime - getSyncInterval()) >=0;
if (!shouldSync && getSyncInterval()!=Constants.INVALID_ITEM){
Log.i("Current time = " + currentTime + " last sync = " + getLastSyncTimeStamp() + " sync interval = " + getSyncInterval());
Log.i("Difference = " + (netTime - getSyncInterval()));
return;
}*/
if (user.isUserLoggedIn() && (!TextUtils.isEmpty(user.peekLoggedInUserAccountToken(null)))){
startService(api.getGlobalStream(0,10,globalStreamRefreshReciever));
startService(api.getNewMessagesInbox(newMessagesReciever));
startService(api.getViewedMessagesInbox(false, getViewedMessagesReciever));
addTimeStamp();
Log.i("in sync");
try {
latch.await(1, TimeUnit.MINUTES);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
Log.e("Error in latch while sync ");
}
}
}
Upvotes: 5
Views: 2079
Reputation: 650
when we have ContentObserver registered, the sync adapter will go in loop even after setting syncToNetwork as false.
notifyChange(uri, null, false);
Upvotes: 0
Reputation: 11453
You're missing a lot of code there, hard to find your problem when you don't tell us what you're doing.
Going out on a limb with a guess at your problems...
Do either addTimeStamp()
or the various services you create modify the data stored in your ContentProvider?
If so, does your ContentProvider call ContentResolver.notifyChange(uri, null)
?
If so, your ContentProvider notifies Android that it has changed and needs a sync, thus driving a loop.
The API is notifyChange (Uri uri, ContentObserver observer, boolean syncToNetwork)
. you need to call with notifyChange(uri, null, false);
-- This indicates that you've pulled a change from the network and that it should not be pushed back to the network, thus breaking the loop.
Upvotes: 14