简体   繁体   中英

Infinite loop of SyncAdapter

Yesterday I tested my app in some Virtual devices in Genymotion, and I realized some times, App sends infinite sync requests to the server on some devices (all of them were API<21). What's the problem?

Let's give some information about the project: I used SyncAdapter and Room Persistence in my project. As I read on android documents, I have to use ContentProvider for access to Database from SyncAdapter. But I left ContentProvider empty and connected to Room from SyncAdapter directly. It's some of the project codes that may help you imagine operations:

SyncAdapter class:

public class SyncAdapter extends AbstractThreadedSyncAdapter {
  public SyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
  }
  public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
    super(context, autoInitialize, allowParallelSyncs);
  }
  @Override public void onPerformSync(Account account, Bundle extras, String authority,
      ContentProviderClient provider, SyncResult syncResult) {
    if (!AppCheckUtils.appInForeground(getContext())) {
      SyncDataWithServer.sendRequest(getContext());
    }
  }
}

ContentProvider class:

public class DataContentProvider extends ContentProvider {
  @Override public boolean onCreate() {
    return true;
  }

  @Nullable @Override
  public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
      @Nullable String[] selectionArgs, @Nullable String sortOrder) {
    return null;
  }
  @Nullable @Override public String getType(@NonNull Uri uri) {
    return null;
  }
  @Nullable @Override public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
    return null;
  }
  @Override public int delete(@NonNull Uri uri, @Nullable String selection,
      @Nullable String[] selectionArgs) {
    return 0;
  }
  @Override
  public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
      @Nullable String[] selectionArgs) {
    return 0;
  }
}

AndroidManifest.xml:

...
    <provider
        android:name=".contentProvider.DataContentProvider"
        android:authorities="@string/syncContentProvider"
        android:exported="false"
        android:syncable="true"/>
...

SyncAdapter.xml:

<?xml version="1.0" encoding="utf-8"?>
<sync-adapter
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="@string/console_account"
    android:allowParallelSyncs="false"
    android:contentAuthority="@string/syncContentProvider"
    android:isAlwaysSyncable="true"
    android:supportsUploading="false"
    android:userVisible="false"/>

SyncDataWithServer class:

public class SyncDataWithServer {
  private static RESTConnector<SyncResult> messagesREST;
  private static final Object lockObject = new Object();
  public static void sendRequest(Context context) {
    synchronized (lockObject) {
      if (messagesREST == null)
        messagesREST =
            new RESTConnector<>(SendTokenCondition.USERTOKEN__TEMPCODE, false, (ToastErrMsg) null,
                true, 0);
    }
    if (BasicAuth.hasTokenOrTempCode()) {
      if (SerCons.BASE_ST.contains("twitch.tv")) {
        return;
      }
      SettingDataDaoHnd
          .getSyncSettings(context, syncSettings -> sendRequest(context, syncSettings));
    }
  }
  private static void sendRequest(Context context, SyncSettingsFromDB syncSettings) {
    ...
  }
}

I found the problem. When I call accountManager.addAccount(..) or accountManager.removeAccount(..) or accountManager.setPassword, it cause to call syncAdapter.onPerformSync(..) at then sendRequest to server , ... . It causeses infinity loop. I solved the issue with adding extras to Bundle when calling ContentResolver

Bundle bundle = new Bundle();
bundle.putBoolean(IntentCons.SYNC_ADAPTER_DO_SYNC, true);
ContentResolver.addPeriodicSync(account, authority, bundle, syncPeriod);

then in onPerformSync:

  @Override public void onPerformSync(Account account, Bundle extras, String authority,
      ContentProviderClient provider, SyncResult syncResult) {
    boolean doSync = extras.containsKey(IntentCons.SYNC_ADAPTER_DO_SYNC) && extras.getBoolean(IntentCons.SYNC_ADAPTER_DO_SYNC);
    if (doSync) {
      SyncDataWithServer.sendRequest(getContext());
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM