Reputation: 159
So I have been having this problem where onPerformSync is called first when I explicitly call requestSync and secondly after 30 or 60 seconds but mostly 60 seconds, its weird. I am using a ContentObserver and this behaviour is only happening when I use a ContentObserver. I tried calling requestSync directly from my content provider and no additional onPerformSync were triggered. My code excerpts are listed below.
Provider calls notifyChange
@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
Log.d(LOG_TAG, "Inserting with uri " + uri + " with values " + values.toString());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int match = sUriMatcher.match(uri);
....
// This direct requestSync call does not trigger extra syncs
//Bundle extras = new Bundle();
//extras.putString(SyncAdapter.CHANGED_URI, uri.toString());
//getContext().getContentResolver().requestSync(SyncAccount.getAccount(), Contract.CONTENT_AUTHORITY, extras);
getContext().getContentResolver().notifyChange(contentUri, null);
return contentUri;
}
ContentObserver with onChange
public class TableObserver extends ContentObserver {
private final static String LOG_TAG = TableObserver.class.getSimpleName();
private final Account mAccount;
public TableObserver(Handler handler, Account account) {
super(handler);
mAccount = account;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
Log.d(LOG_TAG, "Provider changed here: " + uri.toString());
Bundle args = new Bundle();
args.putString(SyncAdapter.CHANGED_URI, uri.toString());
ContentResolver.requestSync(mAccount, Contract.CONTENT_AUTHORITY, args);
}
}
Sync adapter with onPerformSync
public class SyncAdapter extends AbstractThreadedSyncAdapter {
private static final String LOG_TAG = SyncAdapter.class.getSimpleName();
public static final String CHANGED_URI = "changed_uri";
private ContentResolver mContentResolver;
public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
mContentResolver = context.getContentResolver();
}
public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
super(context, autoInitialize, allowParallelSyncs);
mContentResolver = context.getContentResolver();
}
// This is automatically performed in a background thread.
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.d(LOG_TAG, "Synchronizing: " + extras.get(CHANGED_URI));
}
}
Upvotes: 1
Views: 595
Reputation: 159
Fixed it by passing false to notifyChange which does removes the sync to network behaviour of the method like so:
getContext().getContentResolver().notifyChange(contentUri, null, false);
Upvotes: 3