Android SyncAdapter 陷入无限同步循环
我正在编写一个 Android 同步适配器,基本上在无限循环中同步时遇到问题。同步完成后,一切都会重新开始。
谢谢你,
问候,
阿克谢
@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 ");
}
}
}
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 ");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在那里丢失了很多代码,当你不告诉我们你在做什么时很难找到你的问题。
大胆猜测您的问题...
addTimeStamp() 或您创建的各种服务是否会修改 ContentProvider 中存储的数据?
如果是这样,您的 ContentProvider 是否调用
ContentResolver.notifyChange(uri, null)
?如果是这样,您的 ContentProvider 会通知 Android 它已更改并需要同步,从而驱动循环。
API 是
notifyChange (Uri uri, ContentObserverobserver, booleansyncToNetwork)
。您需要使用notifyChange(uri, null, false);
进行调用 - 这表明您已从网络中提取更改,并且不应将其推回网络,从而破坏环形。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 withnotifyChange(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.当我们注册了ContentObserver时,即使将syncToNetwork设置为false,同步适配器也会进入循环。
when we have ContentObserver registered, the sync adapter will go in loop even after setting syncToNetwork as false.