SyncAdapter的无限循环

时间:2019-03-18 08:06:47

标签: android android-room android-architecture-components android-syncadapter

昨天,我在Genymotion的某些虚拟设备中测试了我的应用程序,后来我意识到,App向某些设备上的服务器发送无限同步请求(所有这些请求都是API <21)。有什么问题吗?

让我们提供有关该项目的一些信息: 我在项目中使用了SyncAdapter和Room Persistence。 在阅读android文档时,我必须使用ContentProvider从SyncAdapter访问数据库。但是我将ContentProvider保留为空,并直接从SyncAdapter连接到Room。一些项目代码可以帮助您想象操作:

SyncAdapter类:

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类:

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类:

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) {
    ...
  }
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。 当我调用accountManager.addAccount(..)或accountManager.removeAccount(..)或accountManager.setPassword时,它将导致在调用sendServer的syncAdapter.onPerformSync(..)到服务器时,...。这会导致无限循环。 我通过在调用ContentResolver时在Bundle中添加了额外功能解决了该问题

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

然后使用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());
    }
  }