境界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();
}
}
我的步骤:
- 入门Android应用
- 检查的方法是RealmChangeListener.onChange()调用了
- 卸载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.
问题:
- 为什么在某些设备/仿真器的方法RealmChangeListener.onChange()不要拨打10次?
- 我怎样才能解决这个问题?
谢谢。