0

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 )
}
Jude Fernandes
  • 7,437
  • 11
  • 53
  • 90
  • Try removing the change listener from the previous results, and add change listener to the new results. – EpicPandaForce Nov 03 '18 at 22:13
  • @EpicPandaForce I've updated the question with a bit at the end that gives you an idea of what I'm trying to achieve, maybe there is a different approach that I could take, I understand that realm fires listeners when there are changes to the underlying data but in my case the data is the same, it's just the queries that change and I want to be able to listen when the results of those change, in a single place in the app rather than scattering it all over the place. – Jude Fernandes Nov 04 '18 at 12:10

0 Answers0