Context
I'm working on an offline first app, and I'm basically working on a screen that is supposed to show a list of store offers - discounts, coupons, things like that - I'm trying to rely on Realm queries via Flowables to fetch the data, and I want the app to be 100% honest with what we have on the Realm DB. I mean, that if something changes in the database the app should immediately refresh the UI, that's why I'm using Flowables.
Problem
This approach has been working fine so far, but I've stumbled across this specific situation where things are a bit tricky. I need to display this list of store offers like this:
private val offersFlowable: Flowable<RealmResults<RealmOffer>>
get() {
val realm = Realm.getDefaultInstance()
return realm.where(RealmOffer::class.java)
.equalTo("userId", CredentialManager.getInstance().getUserId())
.and()
.beginGroup()
.isNotNull("expiryDate").and().greaterThan("expiryDate", Date())
.endGroup()
.findAll()
.asFlowable()
}
The query itself works perfectly, I fetch the user's offers which haven't expired yet. The only problem is that it seems I need to recreate this query every time I want to refresh the data. Why? Because of the Date(). I open the Activity that renders this RecyclerView list of store offers, I instantiate the query and start listening for the changes - so far so good - but if I have a store offer that is about to expire in a minute or so, wait that full minute and do a Pull To Refresh, the expire offer doesn't go away. Again, because of the Date() because the current date was instantiated a minute ago and it is still that date.
If I kill the app or instantiate the Activity from scratch, everything works as expected because I'm instantiating the query again, therefore getting an up to date Date().
What I've tried so far
-
Instead of hardcoding that
Date()arg there, having acurrentDateproperty in myViewModel- the place where this Flowable lives - I was kinda hoping that if the date arg was a reference to a computed Kotlin property that would do the trick, but it didn't work. -
Same thing as above but I changed the
expiryDatefield to millis instead of aDate.
The Workaround
So, the only thing that works right now is just re-instantiating the Flowable query every time I do a PTR, that way I pass an up to date Date() arg and the query works as expected.
My Question
Is there any way to fix this without having to re-instantiate the query all the time?
Thanks! Appreciate all feedback! 🙇
from Realm Flowable query filtering by the current date
No comments:
Post a Comment