Android SyncAdapter 陷入无限同步循环

发布于 2024-11-29 17:34:38 字数 2621 浏览 0 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-12-06 17:34:38

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

大胆猜测您的问题...

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 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.

夏夜暖风 2024-12-06 17:34:38

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

notifyChange(uri, null, false);

when we have ContentObserver registered, the sync adapter will go in loop even after setting syncToNetwork as false.

notifyChange(uri, null, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文