Reputation: 101
Note: Room checks invalidations based on table modifications, which means it may dispatch false positive notifications. Guide to app architecture
What is an invalidation for Room
? How does it cause false positive notifications?
Upvotes: 0
Views: 95
Reputation: 6872
That means,
Suppose you have below query
@Query(“SELECT * FROM Users WHERE userId = :id)
fun getUser(id: String): LiveData<User>
And you are observing it like
getUser("id_1").observe(this, Observer{
// do something
})
There is nothing wrong in above method, but there is the case of false positive notifications.
Now suppose that from somewhere else you have deleted user with an userId = "id_2"
. At this point, you know that you don't need to get notified for your earlier getUser("id_1")
call as it has nothing to do with your operations on id_2
. But you still will get notified and your // do something
will run again. That's because, Room will know that something has been changed, but doesn't know what has been changed and hence it will just re-query and send the result back again.
In order to bypass this false positive notification, you can use MediatorLiveData
when you have LiveData
as return type or distinctUntilChanged
if you are have RxJava
as return type in your Daos.
Ref: 7 Pro-tips for Room
Upvotes: 2