启动Android应用时,我想:
这里代码:
public class MainApp extends MultiDexApplication {
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) {
setPermissions(user);
}
SyncCredentials syncCredentials = SyncCredentials.usernamePassword(
BuildConfig.USER_NAME, BuildConfig.PASSWORD, false);
SyncUser.loginAsync(syncCredentials, authUrl, callback);
}
private void setPermissions(final SyncUser syncUser) {
Realm managementRealm = syncUser.getManagementRealm();
String offerToken = BuildConfig.REALM_OFFER_TOKEN;
final PermissionOfferResponse permissionOfferResponse = new PermissionOfferResponse(offerToken);
// Add to management Realm to sync with ROS
managementRealm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.insert(permissionOfferResponse);
}
});
RealmResults<PermissionOfferResponse> realmResultPermissionOfferResponse = managementRealm.where(PermissionOfferResponse.class)
//.equalTo("id", offerId)
.equalTo("statusCode", 0) // status > 0 is error
.findAll();
realmResultPermissionOfferResponse.addChangeListener(new RealmChangeListener<RealmResults<PermissionOfferResponse>>() {
@Override
public void onChange(RealmResults<PermissionOfferResponse> offers) {
// call only once
PermissionOfferResponse offer = offers.first();
String realmURL = offer.getRealmUrl();
}
});
}
当我第一次启动应用程序 onChange()成功调用时,结果我从 PermissionOfferResponse 获取 realmURL 。行。
但是,当我再次启动app时,方法 onChange() NOT CALL。结果我无法获得RealmURL。