5

私は Android Sync Adapter を書いていますが、基本的に無限ループでの同期に問題があります。同期が完了するとすぐに、最初からやり直します。

ありがとうございました、

よろしく、

アクシャイ

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

        }
    }
4

2 に答える 2

14

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.

于 2011-11-16T14:31:02.100 に答える