2011-08-16 12 views
5

Sto scrivendo un adattatore Android Sync e fondamentalmente ho un problema con esso la sincronizzazione in un ciclo infinito. Non appena la sincronizzazione viene completata, ricomincia da capo.Android SyncAdapter bloccato nel ciclo di sincronizzazione infinito

Grazie,

saluti,

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

     } 
    } 
+0

possibile duplicato del [Prevenire ciclo di sincronizzazione di rete durante la sincronizzazione dalla rete in Android ContentProvider] (http://stackoverflow.com/questions/6588770/prevent-network-sync-loop-when-syncing-from-network -in-android-contentprovider) – jcwenger

risposta

14

Stai perdendo un sacco di codice lì, è difficile trovare il problema quando ci non dite che cosa' stai facendo.

Andando su un arto con un'indovinare i tuoi problemi ...

Eseguire una addTimeStamp() oi vari servizi creati modificare i dati memorizzati nel ContentProvider?

In tal caso, il vostro ContentProvider chiama ContentResolver.notifyChange(uri, null)?

In tal caso, ContentProvider notifica ad Android che è stato modificato e richiede una sincronizzazione, guidando così un ciclo.

L'API è notifyChange (Uri uri, ContentObserver observer, boolean syncToNetwork). è necessario chiamare con notifyChange(uri, null, false); - Ciò indica che è stata effettuata una modifica dalla rete e che non dovrebbe essere reinserita nella rete, interrompendo così il ciclo.

+0

+1 mi hai salvato dal diventare completamente pazzo ... Non riuscivo a capirlo perché il codice di notifica change è stato sepolto in profondità sotto copertura;) – Merlin

0

quando ContentObserver è registrato, l'adattatore di sincronizzazione verrà eseguito in loop anche dopo aver impostato syncToNetwork come falso.

notifyChange(uri, null, false);