Jude Fernandes
Jude Fernandes

Reputation: 7517

Listener for Realm Query changes

I have the following code in my activity where I am trying to fetch data from realm and then display it in a list. I have used a listener which works perfectly.

private RealmResults<Data> mData = repo.fetchData();
// internally the call is handled by
  realm.where(Data.class).findAllAsync();

mData.addChangeListener(data -> {
    // access to the data stored locally
    // receive updates in case data changes
    // set the data to the list
});

At a later point in the app, I want to also be able to filter the above data based on a user entered search string and receive the results in the same listener. I have tried the following code.

mData = poiRepo.fetchData("query");

That doesn't seem to work, I'm guessing because it returns a new list to which the listener is not attached. Is there a way I can listen for changes in the result of a realm query when the underlying data has not changed or any other way?

What I am trying to achieve.

mData.addChangeListener(data -> {
    // single place where filtered data is delivered and sent to recycler view
});

function a(){
    repo.fetchData( //filter parameters )
}

function b(){
    repo.fetchData( //filter parameters )
}

function c(){
    repo.fetchData( //filter parameters )
}

Upvotes: 0

Views: 321

Answers (0)

Related Questions