To listen to changes in RealmList
onChange should get called when new item is added to the list
onChange does not gets called sometimes when new item is added
Tried various scenarios. cannot pinpoint the problem exactly as the behavior is random.
Realm version(s): 3.1.3
Realm sync feature enabled: no
Android Studio version: 2.3
Which Android version and device: checked on 5.0 and 6.0
Hi @SarthakM9 Without something more concrete to on, it is hard for us to do anything about this. Can you reproduce this is in a sample project? Even if it only happens once in a while that would be invaluable.
Hello @cmelchior, yes I am continuously able to reproduce this issue in a project. And it happens quite frequently. let me know if I could assist you in any way.
@SarthakM9 If you can provide a sample project. We will be happy to take a look at it.
@cmelchior would code snippets help?
Not really, unless it is a complete snippet, which would fit better in a sample project
@cmelchior sure let me create a sample project and reproduce the issue! :)
I created a sample project and was able to reproduce the issue, it is reproducible on any list size and not just 0 sized list.
Moreover, the issue is quite less frequent in sample project but it is still reproducible.
Sample: https://github.com/SarthakM9/RealmTest
Steps:
1) Open the application
2) Click "add to db button"
Sometimes the listener does not works, so onChange never gets called for all subsequent updates to realm but the entries in db get added normally (as can be seen by closing and opening the app again)
Hi! I tried your project and I can see your problem.
First, please take a look at the this doc https://realm.io/docs/java/latest/#notifications
Then I can totally understand that it is surprising to you that contact.conversation.textMessages returns different object every time. That means when you do:
contact.conversation?.textMessages?.addChangeListener(this)
It is actually adding a listener on an anonymous object. The listener will stop working when the object gets GCed.
Solution is simple, just keep a member of that RealmList in your activity class and add listener on that instead.
We will document this.
Issue is resolved, many thanks! 馃憤 :)
Hello, there's one more thing I would like to ask:
I have a RealmClass named Conversation which has a RealmList with a listener attached to it.
But whenever any other field of Conversation class changes, the change listener which was attached to the RealmList gets invoked.
Is this a bug or am I missing something?
The listener is attached to the RealmList or the Conversation? What is the type of RealmList?
if it is like:
public A extends RealmObject {
String name;
RealmList<B> list;
}
public B extends RealmObject {
A a;
}
If a's name field changes, and a B object in the list contains the A object, listener on the list will be triggered.
Listeners on collection will reported nested link changes as well. But listener on RealmObject won't.
Hello @beeender, yes the dependency is quite similar as you have mentioned.
public Contact extends RealmModel {
// other fields
Conversation conversation;
}
public Conversation extends RealmModel {
// Other fields
RealmList<TextMessage> list;
}
public TextMessage extends RealmModel {
// Other fields
Contact contact;
}
Now, I have a RealmResults
I also have a listener attached to RealmList
So, is there any way to update a value(let's say an integer field) in Conversation class without invoking the RealmList
@SarthakM9 Currently, no. What is your use case? Although the listener will be triggered in this case, the view will be refreshed with the same data. Is there any other side effect than that?
Hello @beeender there's no side effect, listener's are getting triggered properly.
I need to reset a counter variable inside the listener callback, to prevent infinite callbacks, I am first checking if the RealmList has been updated or not.
Hello Realm Team,
Don't know if it's the right place to ask this question but since it might be related to documentation update so asking it here:
In my particular use case, I dont need to update the UI when data changes, just want to show static data.
So I open a realm instance, fetch a RealmList and immediately close the realm instance.
I am still able to read the data from the list.
Is it the right approach or should I keep my realm instance open?
p.s: using Realm v3.5.0
thanks!
So I open a realm instance, fetch a RealmList and immediately close the realm instance.
I am still able to read the data from the list.
You probably have another open Realm instance somewhere which is not closed
@Zhuinden Ah! Got it! Thanks!
Most helpful comment
You probably have another open Realm instance somewhere which is not closed