Android,Realm:PermissionOfferResponse onChange()只调用一次

时间:2017-08-16 15:51:41

标签: android realm

启动Android应用时,我想:

  • 登录Realm对象服务器
  • 接收方创建PermissionOfferResponse
  • 等待服务器同步和处理商品响应,这将填充响应对象的realmUrl属性。

这里代码:

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。

0 个答案:

没有答案