For why this happens and more details check out 7 Pro-tips for Room!
-
-
Save florina-muntenescu/fea9431d0151ce0afd2f5a0b8834a6c7 to your computer and use it in GitHub Desktop.
/* | |
* Copyright (C) 2017 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example.android.observability.persistence.gist | |
import android.arch.lifecycle.LiveData | |
import android.arch.lifecycle.MediatorLiveData | |
import android.arch.lifecycle.Observer | |
/** | |
* LiveData that propagates only distinct emissions. | |
*/ | |
fun <T> LiveData<T>.getDistinct(): LiveData<T> { | |
val distinctLiveData = MediatorLiveData<T>() | |
distinctLiveData.addSource(this, object : Observer<T> { | |
private var initialized = false | |
private var lastObj: T? = null | |
override fun onChanged(obj: T?) { | |
if (!initialized) { | |
initialized = true | |
lastObj = obj | |
distinctLiveData.postValue(lastObj) | |
} else if ((obj == null && lastObj != null) || obj != lastObj) { | |
lastObj = obj | |
distinctLiveData.postValue(lastObj) | |
} | |
} | |
}) | |
return distinctLiveData | |
} |
/* | |
* Copyright (C) 2017 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example.android.observability.persistence.gist | |
import android.arch.lifecycle.LiveData | |
import android.arch.persistence.room.Dao | |
import android.arch.persistence.room.Query | |
import com.example.android.observability.persistence.User | |
import io.reactivex.Flowable | |
/** | |
* Data Access Object for the users table. | |
*/ | |
@Dao | |
abstract class UserDao { | |
/** | |
* Get a user by id. | |
* @return the user from the table with a specific id. | |
*/ | |
@Query("SELECT * FROM Users WHERE userid = :id") | |
protected abstract fun getUserById(id: String): LiveData<User> | |
fun getUserByIdDistinctLiveData(id: String): LiveData<User> = getUserById(id).getDistinct() | |
/** | |
* Get a user by id. | |
* @return the user from the table with a specific id. | |
*/ | |
@Query("SELECT * FROM Users WHERE userid = :id") | |
protected abstract fun getUserByIdFlowable(id: String): Flowable<User> | |
fun getUserByIdDistinctFlowable(id: String): Flowable<User> = | |
getUserByIdFlowable(id) | |
.distinctUntilChanged() | |
} |
Does it keep another copy of the result in memory? Is there an alternative if there are large data sets that need to be queried and we don't want to keep their copies?
+1
Very nice solution
Here's a smaller kotlin version
fun <T> LiveData<T>.getDistinct(): LiveData<T> {
val mediator = MediatorLiveData<T>()
mediator.addSource(this, object: Observer<T> {
private var initialized = false
private var lastObj: T? = null
override fun onChanged(t: T) {
if (initialized && t == lastObj) return
if (!initialized) initialized = true
lastObj = t
mediator.postValue(lastObj)
}
})
return mediator
}
Any idea to avoid false positive notifications for observable queries in case of list !.
I want something like this
@query("SELECT * FROM Users WHERE username = :name")
protected abstract fun getUsersByNameFlowable(name String)
: Flowable<List>
fun getUsersByNameDistinctFlowable(id: String): Flowable<List<User>> =
getUsersByNameFlowable(id) .distinctUntilChanged()
distinctUntilChanged considers subsequent items for comparison not for list i guess !.
Any solution to work around this issue ?
Does it keep another copy of the result in memory? Is there an alternative if there are large data sets that need to be queried and we don't want to keep their copies?
+1
Instead of an object you can save its type and identifier and compare only mutable data.
You can use instead Transformations.distinctUntilChanged
provided by androidx
So code will look like this
@Dao
abstract class UserDao {
@Query("SELECT * FROM Users WHERE userid = :id")
protected abstract fun getUserById(id: String): LiveData<User>
fun getUserByIdDistinctLiveData(id: String): LiveData<User> = Transformations.distinctUntilChanged(getUserById(id))
}
You can use instead
Transformations.distinctUntilChanged
provided by androidxSo code will look like this
@Dao abstract class UserDao { @Query("SELECT * FROM Users WHERE userid = :id") protected abstract fun getUserById(id: String): LiveData<User> fun getUserByIdDistinctLiveData(id: String): LiveData<User> = Transformations.distinctUntilChanged(getUserById(id)) }
+1
same for Flowable distinctUntilChanged
solved my issue.
For java create an abstract class as follows
To be used as follows