diffutil areContentsTheSame function compares only new items

652 Views Asked by At

I am using AsyncListDiffer in my project, in which i have lots of quotes in main page

Below is the code i've written in my recyclerview adapter

private val diffCallback = object : DiffUtil.ItemCallback<Quote>() {
        override fun areItemsTheSame(oldItem: Quote, newItem: Quote): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Quote, newItem: Quote): Boolean {
           Log.d("mytag", "$oldItem , $newItem")
        return oldItem == newItem
    }

}

When i change something in quote object, i then submit it to the ListDiffer. And as you can see i am logging newItem and oldItem inside areContentsTheSame function. Here both oldItem and newItem are the same objects( i mean their contents are the one i updated recently), which i think oldItem should give me old non-updated item, but it doesn't

(I already tried to make copy of the list and then submit it to differ, it doesn't work)

fun updateQuote(quoteId :String,newQuote: Map<String,String>) = viewModelScope.launch(Dispatchers.IO) {
        _updateQuote.postValue(DataState.Loading())
        val response = mainRepository.updateQuote(quoteId, newQuote)
        val handledResponse = handleQuoteResponse(response)
        val quoteToUpdate = quoteList.find { quote -> quote.id == quoteId}
        quoteToUpdate?.genre = newQuote["genre"]
        quoteToUpdate?.quote = newQuote["quote"]
        _quotes.postValue(DataState.Success(QuotesResponse(quoteList.map { it.copy() })))
        _updateQuote.postValue(handledResponse)
    }
1

There are 1 best solutions below

0
On

I saw several posts that helped me with this problem. But in the end my problem was that I was comparing a variable of type var in my data class. I think that somehow she was keeping the reference to the old list. So I advise anyone who sees this answer to look for places where their variables or lists can have the same reference. Ex: Mutable list, var, etc...