Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot modify managed objects outside of a write transaction ( Realm + Android )

I have implement the following method to update UserProfile from realm db.

override fun updateCurrentUser(userProfile: UserProfile, successAction: () -> Unit) {
        val realm = Realm.getDefaultInstance()
        realm.executeTransactionAsync(
                object : Realm.Transaction {
                    override fun execute(realm: Realm) {

                        val user = realm.where(UserProfile::class.java).equalTo("userName", userProfile.userName).findFirst()
                        user?.firstName = userProfile.firstName
                        user?.lastName = userProfile.lastName
                        user?.gender = userProfile.gender
                        user?.height = userProfile.height
                        user?.weight = userProfile.weight
                        user?.dob = userProfile.dob
                        user?.firstTimeSetupDone = true

                    }
                },
                object : Realm.Transaction.OnSuccess {
                    override fun onSuccess() {
                        Log.i(TAG, "Saved User successfully with $userProfile")
                        successAction()
                    }

                },
                object : Realm.Transaction.OnError {
                    override fun onError(error: Throwable?) {
                        Log.e(TAG, "Error saving user", error)
                    }
                }
        )
    }

But it throws the following error even though I'm executing it under realm.executeTransactionAsync

java.lang.IllegalStateException: Cannot modify managed objects outside of a write transaction.
                                                                            at io.realm.internal.Table.throwImmutable(Table.java:674)
                                                                            at io.realm.internal.Table.checkImmutable(Table.java:549)
                                                                            at io.realm.internal.UncheckedRow.setString(UncheckedRow.java:230)
                                                                            at io.realm.UserProfileRealmProxy.realmSet$firstName(UserProfileRealmProxy.java:209)
                                                                            at saukya.wellness.mobile.services.data.domain.UserProfile.setFirstName(UserProfile.kt:27)
                                                                            at saukya.wellness.mobile.ui.onboarding.OnBoardingPresenter.submitData(OnBoardingPresenter.kt:102)
                                                                            at saukya.wellness.mobile.ui.onboarding.OnBoardingActivity.submitData(OnBoardingActivity.kt:106)
                                                                            at saukya.wellness.mobile.ui.onboarding.OnBoardingHealthInfoFragment$setupNextButton$1.onClick(OnBoardingHealthInfoFragment.kt:86)
                                                                            at android.view.View.performClick(View.java:5646)
                                                                            at android.view.View$PerformClick.run(View.java:22459)
                                                                            at android.os.Handler.handleCallback(Handler.java:761)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                            at android.os.Looper.loop(Looper.java:156)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6523)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
like image 544
kokilayaa Avatar asked Jan 03 '23 18:01

kokilayaa


1 Answers

Object queried by realm can't be modified, simple way to solve this problem is to new a object, copy realm object fields to this new one. If you want to persist,call insertOrUpdate method to save it in realm.

like image 79
L. 文顷 Avatar answered Jan 05 '23 15:01

L. 文顷