Android SyncAdapter卡在无限同步循环中

时间:2011-08-16 15:08:23

标签: android 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 ");
            }

        }
    }

2 个答案:

答案 0 :(得分:14)

你错过了很多代码,当你不告诉我们你在做什么时很难找到你的问题。

对你的问题进行猜测......

您创建的addTimeStamp()或各种服务是否会修改ContentProvider中存储的数据?

如果是这样,您的ContentProvider会调用ContentResolver.notifyChange(uri, null)吗?

如果是这样,您的ContentProvider会通知Android它已更改并需要同步,从而驱动循环。

API为notifyChange (Uri uri, ContentObserver observer, boolean syncToNetwork)。您需要使用notifyChange(uri, null, false);进行呼叫 - 这表示您已从网络中进行了更改,并且不应将其推回网络,从而打破了循环。

答案 1 :(得分:0)

当我们注册了ContentObserver时,即使将syncToNetwork设置为false,同步适配器也会进入循环。

notifyChange(uri, null, false);