安卓同步适配器陷入无限同步循环

5
我正在编写一个Android同步适配器,但是在同步时遇到了无限循环的问题。一旦同步完成,它就会重新开始。
谢谢,
问候,
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 ");
            }

        }
    }

2个回答

14

你缺少很多代码,如果不告诉我们你正在做什么,很难找到你的问题。

猜测你的问题...

你创建的 addTimeStamp() 或其他服务是否修改了存储在 ContentProvider 中的数据?

如果是这样,你的 ContentProvider 是否调用了 ContentResolver.notifyChange(uri, null)

如果是这样,你的 ContentProvider 会通知 Android 数据已更改,需要进行同步,从而驱动一个循环。

API 是 notifyChange (Uri uri, ContentObserver observer, boolean syncToNetwork)。你需要使用 notifyChange(uri, null, false);-- 这表示你从网络中拉取了更改,并且不应将其推回到网络中,从而打破循环。


+1 你救了我免于彻底疯掉...我无法解决它,因为notifychange代码被深深地隐藏了;) - Moog

0
当我们注册了ContentObserver时,即使将syncToNetwork设置为false,同步适配器仍会进入循环。
notifyChange(uri, null, false);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接