question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RealmChangeListener on empty RealmResults never fires

See original GitHub issue

Goal

Receive change updates on a RealmQuery

Expected Results

When the RealmQuery changes, the RealmChangeListener is fired

Actual Results

The RealmChangeListener is never fired

Steps & Code to Reproduce

I create a query (realm.where(Test.class).findAll()), and add a RealmChangeListener to the returned RealmResults (I maintain a strong reference to the RealmResults).

If the query was empty when I made it, the RealmChangeListener never fires, no matter how many times I insert an object that matches the query after that. If there is an object when the query is made, the the RealmChangeListener does fire, even if all the objects are removed and then another one is inserted.

Version of Realm and tooling

Realm version(s): 3.3.1

Realm sync feature enabled: no

Android Studio version: 3.0 Canary 3

Which Android version and device: Pixel 7.1.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:25 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
Zhuindencommented, Jun 20, 2018

See https://realm.io/docs/java/latest/api/io/realm/RealmResults.html

Adds a change listener to this RealmResults. Registering a change listener will not prevent the underlying RealmResults from being garbage collected. If the RealmResults is garbage collected, the change listener will stop being triggered. To avoid this, keep a strong reference for as long as appropriate e.g. in a class variable.

 public class MyActivity extends Activity {

     private RealmResults<Person> results; // Strong reference to keep listeners alive

     @Override
     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       results = realm.where(Person.class).findAllAsync();
       results.addChangeListener(new RealmChangeListener<RealmResults<Person>>() {
           @Override
           public void onChange(RealmResults<Person> persons) {
               // React to change
           }
       });
     }
 }

On top of that, you’re calling copyFromRealm instead of, like, copyToRealmOrUpdate or insertOrUpdate or anything that actually writes into the db. Also, running synchronous transaction inside a change listener?

Are you trying to get an infinite loop? 😕

What are you doing???

1reaction
eygrabercommented, Jun 27, 2017

@kneth sorry I haven’t got a chance to try. Crazy sprints at work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Realm Change Listener with RealmResults not being called
You need to store the RealmResults as a field reference in order to ensure that Realm can update it RealmResults<Record> realmResults ...
Read more >
RealmResults (Realm 6.0.0) - MongoDB
A RealmResults object cannot be passed between different threads. Notice that a RealmResults is never null not even in the case where it...
Read more >
RealmResults (Realm 1.1.0)
A RealmResults object cannot be passed between different threads. Notice that a RealmResults is never null not even in the case where it...
Read more >
Creating a Reactive Data Layer with Realm and RxJava2
RealmChangeListener <RealmResults<Task>> listener = new ... Of course, this is also why onCompleted() was never called - a listener doesn't ...
Read more >
The things I've learned using Realm | by Gabor Varadi - Medium
And if you ever truly do need to swap out one implementation for the other, ... you receive a so-called RealmResults , which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found