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 ");
}
}
}