2014-09-21 3 views
5

La mia applicazione ha un adattatore di sincronizzazione creato seguendo la struttura mostrata dallo Android developer site. Il metodo onPerformSync() ottiene i dati da internet e lo inserisce nel database con un inserimento di massa:onPerformSync() chiamato più volte

Vector<ContentValues> cVVector = new Vector<ContentValues>(rssItems.size()); 

for(RssItem rssItem : rssItems) { 
    ContentValues newsValues = new ContentValues(); 
    // Get data from the remote server 
    // Fill all the values 
    newsValues.put(...); 
    // Add the values to a vector, at the end a BulkInsert will be called 
    cVVector.add(newsValues); 
} 
mContext.getContentResolver().bulkInsert(NewsEntry.CONTENT_URI, cvArray); 

Il database ha un IGNORARE politica quando si occupa di conflitti:

final String SQL_CREATE_NEWS_TABLE = "CREATE TABLE " + NewsEntry.TABLE_NAME + " (" + 
       NewsEntry._ID + " INTEGER PRIMARY KEY," + 
       NewsEntry.COLUMN_NEWS_TITTLE + " TEXT UNIQUE NOT NULL, " + 
       NewsEntry.COLUMN_NEWS_CONTENT + " TEXT NOT NULL, " + 
       NewsEntry.COLUMN_NEWS_DESCRIPTION + " TEXT NOT NULL, " + 
       NewsEntry.COLUMN_NEWS_IMAGE + " TEXT, " + 
       NewsEntry.COLUMN_NEWS_DATE + " TEXT NOT NULL, " + 
       NewsEntry.COLUMN_NEWS_LINK + " TEXT NOT NULL, " + 
       "UNIQUE (" + NewsEntry.COLUMN_NEWS_TITTLE +") ON CONFLICT IGNORE"+ 
       ");"; 

e l'adattatore di sincronizzazione è configurato per eseguire una sincronizzazione ogni 86400 secondi

/** 
* Helper method to schedule the sync adapter periodic execution 
*/ 
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) { 
    Account account = getSyncAccount(context); 
    String authority = context.getString(R.string.content_authority); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     // we can enable inexact timers in our periodic sync 
     SyncRequest request = new SyncRequest.Builder(). 
       syncPeriodic(syncInterval, flexTime). 
       setSyncAdapter(account, authority).build(); 
     ContentResolver.requestSync(request); 
    } else { 
     ContentResolver.addPeriodicSync(account, 
       authority, new Bundle(), syncInterval); 
    } 
} 

Tuttavia, viene chiamato continuamente.

risposta

0

onPerformSync() verrà chiamato solo se viene forzatamente chiamato utilizzando l'API "requestSync" e una volta al momento della creazione dell'account con il bundle vuoto.

0

Mi sono bloccato con lo stesso problema e in fondo ho trovato che ho anche chiamato più volte ContentResolver.requestSync() (prima di ogni onPerformSync()). In altre parole, requestSync() causa la chiamata onPerformSync(). Questo è stato un mio errore e dovrei riconsiderare la logica.

Prova a registrare requestSync() chiamate nel codice e probabilmente riesci a trovare l'errore.