RealmResults<Foo> change listener not called after adding new Foo objects to Realm.
Hey, guys, currently I am enjoying 0.84.2 version of Realm. And I must say, RealmResults ChangeListener, is kinda confusing.
Let's take RealmResults
- I do async find from Realm(listener gets notified when the query completes)
- I update existing object in Foo.class table
But it doesn't get called after I put new new objects of type Foo to Realm.
As your documentation states: "This listener will be called every time the RealmResults is updated to reflect the latest changes in the Realm (usually after a commit)".
So is it a bug or I am missing something?
Thanks in advance.
Hi @Kistamushken I tried to write a unit test according to your scenario, but couldn't reproduce your issue
public void testIssue1802() throws InterruptedException {
final AtomicInteger numberOfCallbackInvocation = new AtomicInteger(0);
final CountDownLatch signalTestFinished = new CountDownLatch(1);
final HandlerThread handlerThread = new HandlerThread("handlerThread");
handlerThread.start();
final Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
final Realm realm = Realm.getInstance(getContext());
realm.beginTransaction();
final Dog dog = realm.createObject(Dog.class);
realm.commitTransaction();
RealmResults<Dog> dogs = realm.where(Dog.class).findAllAsync();
dogs.addChangeListener(new RealmChangeListener() {
@Override
public void onChange() {
if (numberOfCallbackInvocation.incrementAndGet() == 3) {
signalTestFinished.countDown();
}
}
});
// update existing RealmObject should re-run the query
handler.postDelayed(new Runnable() {
@Override
public void run() {
realm.beginTransaction();
dog.setName("Akamaru");
realm.commitTransaction();
}
}, 16);
// adding a new RealmObject should re-run the query
handler.postDelayed(new Runnable() {
@Override
public void run() {
realm.beginTransaction();
realm.createObject(Dog.class);
realm.commitTransaction();
}
}, 32);
}
});
assertTrue(signalTestFinished.await(7, TimeUnit.SECONDS));
}
Does your query involve a link? we do have an existing issue #1717 regarding async query with link
Cheers,
Yep, seems, like it's link issue. Give me time till tomorrow, I will test everything on 0.85 version, and if it's okay I'll let you know.
@kishikawakatsumi thanks - looking forward to your report ;-)
Hey guys, I've created small app to test things out. It uses 0.85. Still no luck in firing RealmChangleListener when adding object to Realm. Listener fires only when query is completed. Important note is that the query doesn't seem to have any links involved.
You are welcome to check the code here: https://github.com/Kistamushken/RealmTests/blob/master/app/src/main/java/com/kistamushken/realmtests/MainActivity.java
Regards,
Hi @Kistamushken thanks for the example, there is a PR to fix this bug https://github.com/realm/realm-java/pull/1809. basically, the current implementation doesn't notify callbacks if you mix in the same thread async queries & commits. In the meantime you can do your commits in a worker thread as well.
please note also that in your example you're using an anonymous class to register a listener, the listener will be garbage collected since we don't hold a strong reference to it. This behaviour will change in the next release https://github.com/realm/realm-java/pull/1725, we will start holding a strong reference to your listeners, but it's your responsibility to remove them when you think it's appropriate (inside onDestroy/onStop for example). This will prevent any memory leak.
realm.removeChangeListener(listener); // to remove a non-anonymous listener
or
realm.removeAllChangeListeners(); // to remove all listeners (including anonymous one)
Here is your example with the suggested modifications
public class MainActivity extends AppCompatActivity implements RealmChangeListener {
Realm realm;
RealmResults<SimpleEntity> simpleEntities;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
realm = Realm.getInstance(this);
simpleEntities = realm.where(SimpleEntity.class).findAllAsync();
simpleEntities.addChangeListener(this);
SimpleEntity simpleEntity;
realm.beginTransaction();
for (int i = 0; i < 20; i++) {
simpleEntity = new SimpleEntity();
simpleEntity.setSomeInt(i);
realm.copyToRealmOrUpdate(simpleEntity);
}
realm.commitTransaction();
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.removeChangeListener(this);
realm.close();
}
public void addToSimpleResults(View view) {
if (simpleEntities == null) return;
final SimpleEntity simpleEntity = new SimpleEntity();
simpleEntity.setSomeInt(simpleEntities.size());
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(simpleEntity);
}
}, null);
}
@Override
public void onChange() {
Toast.makeText(MainActivity.this, "SimpleEntity change listener fired, size = " + simpleEntities.size(), Toast.LENGTH_SHORT).show();
}
}
Cheers,
I see. Thanks for such detailed answer :)
@Kistamushken May we close the issue?
@kneth Yeah thanks.