境界:为什么在设备/仿真器的方法RealmChangeListener.onChange()不是每个

2019-10-28 13:08发布

境界3.5.0

Android应用程序。

我的片段:

public class MainApp extends MultiDexApplication implements RealmChangeListener<RealmResults<PermissionOfferResponse>> {

    private void loginAndConfig() {
        String authUrl = "http://" + BuildConfig.OBJECT_SERVER_IP + ":" + BuildConfig.OBJECT_SERVER_PORT + "/auth";

        SyncUser.Callback callback = new SyncUser.Callback() {
            @Override
            public void onSuccess(SyncUser user) {
                  syncUser = user;
                  createPermissionOfferAndSyncWithROS();
            }

            @Override
            public void onError(ObjectServerError error) {
                String errorMsg;
                switch (error.getErrorCode()) {
                    case UNKNOWN_ACCOUNT:
                        errorMsg = "Account does not exists.";
                        break;
                    case INVALID_CREDENTIALS:
                        errorMsg = "User name and password does not match";
                        break;
                    default:
                        errorMsg = error.toString();
                }
                Log.w(TAG, "loginAndConfig: onError: result errorMsg = " + errorMsg);
            }
        };

        SyncCredentials syncCredentials = SyncCredentials.usernamePassword(
                BuildConfig.USER_NAME, BuildConfig.PASSWORD, false);
        SyncUser.loginAsync(syncCredentials, authUrl, callback);
    }

    private void createPermissionOfferAndSyncWithROS() {
        Realm managementRealm = syncUser.getManagementRealm();
        String offerToken = BuildConfig.REALM_OFFER_TOKEN;
        final PermissionOfferResponse newPermissionOfferResponse = new PermissionOfferResponse(offerToken);
        managementRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.insert(newPermissionOfferResponse);
            }
        });

        RealmResults<PermissionOfferResponse> realmResultPermissionOfferResponse = managementRealm.where(PermissionOfferResponse.class)
                .equalTo("id", newPermissionOfferResponse.getId())
                .equalTo("statusCode", 0) // status > 0 is error
                .findAll();
        realmResultPermissionOfferResponse.addChangeListener(this);
    }

    @Override
    public void onChange(RealmResults<PermissionOfferResponse> permissionOfferResponse) {
        PermissionOfferResponse permissionOfferResponse = permissionOfferResponse.first();
        realmURL = permissionOfferResponse.getRealmUrl();
    }
}

我的步骤:

  1. 入门Android应用
  2. 检查的方法是RealmChangeListener.onChange()调用了
  3. 卸载Android应用

我重复这3个步骤,10倍

结果在不同的Android配置:

******************************************************
Emulator: Android 4.0.3, RAM 512 MB, VM Heap 48 MB
method RealmChangeListener.onChange() call 10 times.

******************************************************
Device: Android 4.3, RAM 700 MB
method RealmChangeListener.onChange() call ONLY 7 times.

******************************************************
Emulator: Android 6.0, RAM 512 MB, VM Heap 48 MB
method RealmChangeListener.onChange() call ONLY 7 times.

******************************************************
Emulator: Android 6.0, RAM 1 GB, VM Heap 48 MB
method RealmChangeListener.onChange() call ONLY 5 times.

******************************************************
Device: Android 6.0, RAM 2 GB
method RealmChangeListener.onChange() call 10 times.

问题:

  1. 为什么在某些设备/仿真器的方法RealmChangeListener.onChange()不要拨打10次?
  2. 我怎样才能解决这个问题?

谢谢。

Answer 1:

下面可以工作:

    private Set<RealmChangeListener<RealmResults<PermissionOfferResponse>> changeListeners = new HashSet<>();

    private void createPermissionOfferAndSyncWithROS() {
        Realm managementRealm = syncUser.getManagementRealm();
        String offerToken = BuildConfig.REALM_OFFER_TOKEN;
        final PermissionOfferResponse newPermissionOfferResponse = new PermissionOfferResponse(offerToken);
        managementRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.insert(newPermissionOfferResponse);
            }
        });

        RealmResults<PermissionOfferResponse> realmResultPermissionOfferResponse = managementRealm.where(PermissionOfferResponse.class)
                .equalTo("id", newPermissionOfferResponse.getId())
                .equalTo("statusCode", 0) // status > 0 is error
                .findAll();

        class _RealmChangeListener implements RealmChangeListener<RealmResults<PermissionOfferResponse>> {
            private RealmResults<PermissionOfferResponse> results;

            public _RealmChangeListener(RealmResults<PermissionOfferResponse> results) {
                this.results = results;
                changeListeners.add(this);
                results.addChangeListener(this);
            }

            @Override
            public void onChange(RealmResults<PermissionOfferResponse> element) {
                changeListeners.remove(this); 
                MainApp.this.onChange(element);
            }
        }
        new _RealmChangeListener(realmResultPermissionOfferResponse);
    }

    public void onChange(RealmResults<PermissionOfferResponse> permissionOfferResponse) {
        PermissionOfferResponse permissionOfferResponse = permissionOfferResponse.first();
        realmURL = permissionOfferResponse.getRealmUrl();
    }
}


文章来源: Realm: Why on Devices/Emulators method RealmChangeListener.onChange() NOT call every times?
标签: android realm