5

My application has a Sync Adapter created following the structure showed by the Android developer site. The onPerformSync() method gets data from the internet and inserts it on the database with a bulk insert:

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

The database has a IGNORE politic when deals with conflicts:

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

And the Sync Adapter is configured to perform a sync every 86400 seconds

/**
 * 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);
    }
}

However, it is called continuously.

Andrew Panasiuk
  • 626
  • 1
  • 8
  • 17
Manuel
  • 2,236
  • 2
  • 18
  • 28

2 Answers2

0

onPerformSync() will be called only if it is forcefully called using "requestSync" api and once at the time of account creation with empty bundle.

0

I got stuck with the same problem and after all I found that I also called ContentResolver.requestSync() multiple times (before every onPerformSync()). In another words, requestSync() causes onPerformSync() call. That was my mistake and I should reconsider the logic.

Try to log requestSync() calls in your code and likely you are able to find the mistake.

Andrew Panasiuk
  • 626
  • 1
  • 8
  • 17