2

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)
    }
nasibeyyubov
  • 1,735
  • 3
  • 11
  • 28
  • 1
    You're probably making a shallow copy of your list so you still end up modifying old items. – Pawel Mar 22 '21 at 13:47
  • what you mean by saying "shallow copy of your list", can you please show an example in the code i've written, or if you need more source code i can paste that here – nasibeyyubov Mar 22 '21 at 13:56
  • https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy – Pawel Mar 22 '21 at 14:01
  • @nesibeyyubov can you confirm that both olditem and newitem hashcodes are same? also can you post result of this ` Log.d("mytag", "$oldItem , $newItem")` and `Quote` class code . if you only modify one item at a time, you can use `notifyItemchanged` to notify the single item view in recycler view, – Rajesh.k Mar 22 '21 at 14:07
  • @Pawel thanks, i got it, i reformatted code so that, i remove the item,save the index of removed item, and add new item at that index and it worked. But is it performance wise to use? is there another way to fix this – nasibeyyubov Mar 22 '21 at 14:21
  • Is this issue fixed? I do have the same issue – Sreehari K Sep 01 '22 at 10:20

1 Answers1

0

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...