使用同步适配器和Room DB对多个实体进行差分向上同步

时间:2018-07-21 11:30:47

标签: android android-room android-syncadapter dirty-data

我在Room DB中保存了多个实体。我想让他们向上同步到服务器。我已经实现了同步适配器并计划了同步。现在,我希望它仅尝试对具有脏数据的实体进行同步。实现这一目标的机制是什么?

SyncUtil:

@TargetApi(Build.VERSION_CODES.FROYO)
    public static void CreateSyncAccount(Context context) {

        boolean newAccount = false;
        boolean setupComplete = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREF_SETUP_COMPLETE, false);

        // Create account, if it's missing. (Either first run, or user has deleted account.)
        Account account = SyncAuthenticatorService.GetAccount(ACCOUNT_TYPE);
        AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
        if (accountManager.addAccountExplicitly(account, null, null)) {
            // Inform the system that this account supports sync
            ContentResolver.setIsSyncable(account, AUTHORITY, 1);
            // Inform the system that this account is eligible for auto sync when the network is up
            ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
            // Recommend a schedule for automatic synchronization. The system may modify this based
            // on other scheduled syncs and network utilization.
            ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), SYNC_INTERVAL);
            newAccount = true;
        }

SyncAdapter:

override fun onPerformSync(account: Account, extras: Bundle, authority: String, provider: ContentProviderClient, syncResult: SyncResult) {

    pref = PreferenceManager.getDefaultSharedPreferences(context)

    Log.e(TAG, "Beginning network synchronization")

    upwardQuizAttempts()
    upwardStudents()
    upwardAttendances()

0 个答案:

没有答案
相关问题