realm-java: isLoaded() seems to be returning an incorrect value.

I have created a project to replicate the issue I am having. https://github.com/chrisolsen/realm-issue

In the onChange listener the RealmObject’s isLoaded() method is returning a true value, but when I then try to bind the data an IllegalStateException is thrown telling me that I should first check it is loaded.

If you run the linked android project the bug will only be seen on the first launch, after that a row will exist in the db thereby avoiding the issue.

Expected Results

isLoaded should initially return false, until the IntentService inserts the data.

Actual Results

isLoaded() returns true before any data exists in the db, causing a crash

07-20 10:14:46.833 5112-5112/com.example.chris.test D/User null?: no
07-20 10:14:46.833 5112-5112/com.example.chris.test D/User loaded?: true
07-20 10:14:46.836 5112-5262/com.example.chris.test D/onHandleIntent: in the service
07-20 10:14:46.863 5112-5112/com.example.chris.test D/AndroidRuntime: Shutting down VM


                                                                      --------- beginning of crash
07-20 10:14:46.863 5112-5112/com.example.chris.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.chris.test, PID: 5112
                                                                      java.lang.IllegalStateException: Can't access a row that hasn't been loaded, make sure the instance is loaded by calling RealmObject.isLoaded().
                                                                          at io.realm.internal.Row$1.getString(Row.java:176)
                                                                          at io.realm.UserRealmProxy.realmGet$name(UserRealmProxy.java:60)
                                                                          at com.example.chris.test.UserData.getName(UserData.java:11)
                                                                          at com.example.chris.test.databinding.ActivityMainBinding.executeBindings(ActivityMainBinding.java:96)
                                                                          at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:355)
                                                                          at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:172)
                                                                          at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:238)
                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                          at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Steps & Code to Reproduce

Run the linked app. The first time the app is run the above crash will occur, after that the app will work as intended.

Code Sample

See https://github.com/chrisolsen/realm-issue

Version of Realm and tooling

Realm version(s): 1.1.0

Android Studio version: 2.1.2

Which Android version and device: Nexus5 emulator (android 6.0), Nexus 7 device (android 6.0)

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 29 (16 by maintainers)

Most upvoted comments

Yes, that sounds plausible. You can use OrderedcollectionChangeSet.isCompleResult(). That will return true once you have downloaded the results from the server.