Android的SyncAdapter停留在无限循环同步 [英] Android SyncAdapter stuck in infinite sync loop

查看:203
本文介绍了Android的SyncAdapter停留在无限循环同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Android同步适配器,基本上具有与其同步在一个无限循环的问题。一旦同步完成这一切又重新开始。

感谢您,

问候,

阿克沙伊

  @覆盖
    公共无效onPerformSync(决算帐户,最后捆绑群众演员,最后弦乐的权威,最终ContentProviderClient提供商,最终SyncResult syncResult){
        Log.i(同步导致完全同步=+ syncResult.fullSyncRequested);
        Log.i(同步结果+ syncResult.toDebugString());
        Log.i(捆绑+ extras.toString());

        最后CountDownLatch闩锁=新CountDownLatch(3);


        最后CachedDataReceiver globalStreamRefreshReciever =新CachedDataReceiver(NULL){
            @覆盖
            保护无效的onComplete(INT结果code){latch.countDown();}
            @覆盖
            保护无效的onError(){latch.countDown();}
        };

        最后CachedDataReceiver newMessagesReciever =新CachedDataReceiver(NULL){
            @覆盖
            保护无效的onComplete(INT结果code){latch.countDown();}
            @覆盖
            保护无效的onError(){latch.countDown();}
        };

        最后CachedDataReceiver getViewedMessagesReciever =新CachedDataReceiver(NULL){
            @覆盖
            保护无效的onComplete(INT结果code){latch.countDown(); showAnyNewInboxItemAlerts(getApplicationContext());}
            @覆盖
            保护无效的onError(){latch.countDown();}
        };


        / *长currentTime的= System.currentTimeMillis的();
        长NETTIME = currentTime的-getLastSyncTimeStamp();
        布尔shouldSync =(NETTIME  -  getSyncInterval())> = 0;
        如果(shouldSync&安培;!&安培;!getSyncInterval()= Constants.INVALID_ITEM){
            Log.i(当前时间=+ currentTime的+上次同步=+ getLastSyncTimeStamp()+同步间隔=+ getSyncInterval());
            Log.i(差=+(NETTIME  -  getSyncInterval()));
            返回;
        } * /



        如果(user.isUserLoggedIn()&安培;&安培;!(TextUtils.isEmpty(user.peekLoggedInUserAccountToken(空)))){
            startService(api.getGlobalStream(0,10,globalStreamRefreshReciever));
            startService(api.getNewMessagesInbox(newMessagesReciever));
            startService(api.getViewedMessagesInbox(假,getViewedMessagesReciever));
            addTimeStamp();
            Log.i(同步);
            尝试 {
                latch.await(1,TimeUnit.MINUTES);
            }赶上(InterruptedException的InterruptedException的){
                interruptedException.printStackTrace();
                Log.e(闩锁,而同步错误);
            }

        }
    }
 

解决方案

您错过了很多code那里,很难找到自己的问题,当你不告诉我们,你在做什么。

走出去的肢体与猜测你的问题......

执行下列任 addTimeStamp()或你创建的各种服务修改存储在您的ContentProvider中的数据?

如果是这样,那么你的ContentProvider电话 ContentResolver.notifyChange(URI,空)

如果这样,您的ContentProvider通知机器人,它已经改变了,需要一个同步,从而驱动一个循环。

该API 有NotifyChange(URI URI,ContentObserver观测,布尔syncToNetwork)。你需要用有NotifyChange调用(URI,空,假); - 这表示你已经将来自网络的变化,它不应该被推迟到网络,从而打破了循环。

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

        }
    }

解决方案

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.

这篇关于Android的SyncAdapter停留在无限循环同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆