Realm-java: Fail to get object on MainThread after created on background

Created on 24 May 2017  Â·  11Comments  Â·  Source: realm/realm-java

Goal

Create realm object from background and get it on mainThread

Expected Results

Got newly created object

Actual Results

Object on mainthread null

Steps & Code to Reproduce

In the sample app: press make transaction to create object -> press "Get item at mainthread" to get the object at mainthread -> app crash

Code Sample

https://github.com/thanhhai08sk/Realm_bug_sample

I include the sample project with real data from my app.
The problem only occurs after I upgrade to realm 3.1.0 and above.
There is no problem when there is little data.
The most stable version from 3.1.0 is 3.1.2 but there is still bug rarely.
There is a lot of bugs but I want to start with this one first.
Sorry for my bad English, please let me know if you want more information.

```

Version of Realm and tooling

Realm version(s): ?

Realm sync feature enabled: yes/no

Android Studio version: 2.3

Which Android version and device: 6.0, xiaomi readmi note 4

T-Duplicate T-Help

Most helpful comment

@beeender Thanks for the explanation. Everything seems to work very well after I try @LingkingObjects. Will update if somethings goes wrong.
Very happy that I post the issue here. Again, thanks for helping me and for creating Realm!

All 11 comments

HI! thanks a lot for the sample project to show the problem. I can reproduce it now. checking...

HI! It is the similar problem with #4544
So here is the procedure when some thing is changed in the background.

  1. Register listeners in UI threadA
  2. Change some data in background threadB
  3. A realm worker threadC starts to compute the change set for the fine grained notifications
  4. threadC finished computing and deliver the notification to threadA.

So this problem happens in the step 3, when there are lots of objects & objects have some deep circular links to each other, the whole computing takes long time. So the UI thread has not been able to get the new data notification to refresh the data version in UI threadA in the step 4.

You can see the discussion in #4544 , we are thinking about introduce a deepLinkDetectLevels to handle cases like this. Before that, you can try to remove some circular relationships in your Object Definition to make the computing faster.
BTW, we introduced reversed link relationship in 3.1.0, by using that, i believe you are able to remove some circular links you have right now and make the objects graph simpler, see https://realm.io/docs/java/3.2.1/api/io/realm/annotations/LinkingObjects.html
The LinkingObjets won't store the links in the file, but just a convenient way to get reversed linked objects. It won't create overhead when computing the change set.

I will just close this issue and dup it to #4544
If you have any general suggestions about this problem, we can discuss it in #4544

Thanks again for the testing project! That saves us lots of time to know what the problem is!

You need to preserve class-level field reference to the RealmResults, otherwise GC will eat them and you won't get notifications.

If the app is slow, consider replacing findAll() with findAllAsync().

@Zhuinden This issue is actually very similar to https://github.com/realm/realm-java/issues/4544#issuecomment-296967988 . So findAllAsync() won't help here :(

Thanks for your quick response.

I think that LinkingObjects can help me solve the problem. Just one question: how can I convert a RealmList field to RealmResult field with the same name?

I try to remove the field then add the realmResult filed with the same name in RealmMigration but it said Realm doesn't support RealmResults field type.

@beeender now that you mention it, how do you add @LinkingObjects to an existing schema? Or does that depend on https://github.com/realm/realm-java/issues/4323 ?

@Zhuinden Adding @LinkingObjects won't trigger a migration, since it actually won't be a persistent property in the schema. You can think it as syntax sugar to create a query for reversed links.

Oh, so not even named backlinks are part of the schema? That's nice.

@Zhuinden See https://github.com/realm/realm-core/blob/master/src/realm/table.cpp#L4519 if you are interested in the implementation.

@beeender Thanks for the explanation. Everything seems to work very well after I try @LingkingObjects. Will update if somethings goes wrong.
Very happy that I post the issue here. Again, thanks for helping me and for creating Realm!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yuwu picture yuwu  Â·  3Comments

Merlin1993 picture Merlin1993  Â·  3Comments

iAbdulaziz picture iAbdulaziz  Â·  3Comments

CNyezi picture CNyezi  Â·  3Comments

wyvern610 picture wyvern610  Â·  3Comments