Android,Realm:每次调用realm.insert(permissionOfferResponse)都需要吗?

时间:2017-08-18 07:32:01

标签: android realm

在Android客户端上,我需要获得realmURL。所以我接下来的步骤:

  1. 登录Realm Object Server(ROS)
  2. 使用收到的令牌创建回复
  3. 处理完成后,offerResponse的realmUrl属性将包含共享领域的URL
  4. 这里摘录:

    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);
                }
    
                @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 setPermissions(final SyncUser syncUser) {
            Realm managementRealm = syncUser.getManagementRealm();
    
            // Create response with received token
            String offerToken = BuildConfig.REALM_OFFER_TOKEN;
            final PermissionOfferResponse permissionOfferResponse = new PermissionOfferResponse(offerToken);
    
            // Add to management Realm to sync with ROS
            managementRealm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                // call every time on start 
                    realm.insert(permissionOfferResponse);
                }
            });
    
            // After processing, offerResponse's realmUrl property will contain the URL of the shared Realm
            // Wait for server to handle the offer
            RealmResults<PermissionOfferResponse> realmResultPermissionOfferResponse = managementRealm.where(PermissionOfferResponse.class)
                    .equalTo("statusCode", 0)
                    .findAll();
            realmResultPermissionOfferResponse.addChangeListener(new RealmChangeListener<RealmResults<PermissionOfferResponse>>() {
                @Override
                public void onChange(RealmResults<PermissionOfferResponse> offers) {
                    PermissionOfferResponse offer = offers.first();
                    String realmURL = offer.getRealmUrl();
                    // success get relmURL
                }
            });
    }
    }
    

    正如您所看到的,此代码从每次应用程序启动开始。每次调用方法 realm.insert(permissionOfferResponse)时都会产生结果。

    问题:我是否需要在启动应用程序时每次调用方法 realm.insert(permissionOfferResponse)

0 个答案:

没有答案