Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update query backing RealmResults for correct notifications when incrementally improving a query. #6216

Open
lucamtudor opened this issue Oct 3, 2018 · 5 comments

Comments

@lucamtudor
Copy link

lucamtudor commented Oct 3, 2018

Describe your problem or use case

I have a search input and a list rendered with RealmRecyclerViewAdapter (or a simple recylerview adapter for that matter, but getting changes from asChangesetObservable()). I want to filter that list based on user's input and get granular notifications for the adapter.

Why am I using Realm? Because the chat-app I'm building is supposed to support lists with 2-3k items and I like the idea of not copying data into memory when rendering 3 of those lists in a viewpager.

What I'm doing right now:

fun observeCampaigns(nameQuery: String): Flowable<RealmResults<CampaignRealm>> =
    Realm.getDefaultInstance().use { realm ->
        realm.where<CampaignRealm>()
            .contains(CampaignRealmFields.NAME, nameQuery, Case.INSENSITIVE)
            .findAllAsync()
            .asFlowable()
            .filter { it.isLoaded }
    }

I'm executing the observeCampaigns function each time the query changes and pass the RealmResults to the adapter.

This doesn't work because I'm creating a new RealmResults each time and I'll always get changeSet.getState() == OrderedCollectionChangeSet.State.INITIAL inside the OrderedRealmCollectionChangeListener of the adapter.

Describe the solution you'd like

I need a way to filter RealmResults, or have the same query re-executed with different parameters so I get proper change notifications.

Additional context

I also wrote a stackoverflow question.

One bypass solution would be to use realm-monarchy to detach realm objects & get an Architecture Components PagedList. This seems counterintuitive and defeats the purpose of using Realm in the first place. I also made an issue on realm-monarchy for feedback.

I'm sorry for the ranty tone, but this seems such a trivial thing & I can't be the first person who wants to do this, right?

@Zhuinden
Copy link
Contributor

Zhuinden commented Oct 4, 2018

In retrospect, it sounds really nice and also more efficient to filter a RealmResults in place, instead of creating a new RealmResults each time and hoping for GC to eliminate previously created result sets that may or may not have to be updated when a new version arrives (background thread write)

@cmelchior
Copy link
Contributor

Yes, I can definitely see the use case for filter UI's that incrementally changes while you want to retain nice animations.

You are right that this is not supported very well in the current Java API's. I believe we discussed the use case internally at some point, but I cannot remember the conclusion right now, so I'll have to dig it up

@lucamtudor
Copy link
Author

@cmelchior any updates? 😄

@cmelchior
Copy link
Contributor

Yes, sorry for not writing it up.

After a little digging, we don't have direct support for it right now, but nothing in the notification implementation should prevent us from doing this.. However, I cannot give you a concrete timeline at this point.

For the matter of API I'm thinking something along the line of:

RealmResults.updateQuery(Query);

Example:
RealmResults<Person> p = realm.where(Person.class).startsWith("name", "chr").findAllAsync();
p.addChangeListener(...);
p.updateQuery(realm.where(Person.class).startsWith("name", "christian"));

@realm-probot
Copy link

realm-probot bot commented Oct 9, 2018

Hey - looks like you forgot to add a T:* label - could you please add one?

@cmelchior cmelchior changed the title Filter RealmResults & get granular notifications (for RecyclerView animations) Update query backing RealmResults for correct notifications when incrementally improving a query. Oct 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants