For why this happens and more details check out 7 Pro-tips for Room!
Last active
February 27, 2023 06:04
-
-
Save florina-muntenescu/fea9431d0151ce0afd2f5a0b8834a6c7 to your computer and use it in GitHub Desktop.
Avoid false positives notifications for observable queries - https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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() | |
} |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of an object you can save its type and identifier and compare only mutable data.