airbnb / epoxy

Epoxy is an Android library for building complex screens in a RecyclerView
https://goo.gl/eIK82p
Apache License 2.0
8.51k stars 728 forks source link

Immutable Exception in Data class, Normal Class #1247

Open sulemankhan4 opened 2 years ago

sulemankhan4 commented 2 years ago

I have a data class

data class NewHomeDataModel(
        var viewType: Int = -1,
        var displayTitle: String? = null,
        var dataList: ArrayList<NewHomeDataItem>? = null,
        var chipList: ArrayList<NewHomeChipBucket>? = null,
        var isApiCallDone: Boolean = false
)

var homeList = ArrayList()

Basically for a specific index, im updating data List , using below code.

val brandDataList = HomeRepository.fetchShopByBrandData(urlParams) val newHomeDataList = newHomeDataModelLiveData.value val shopByBrandItem = Gson().fromJson( Gson().toJson(newHomeDataList?.get(position)), NewHomeDataModel::class.java) val chipList = shopByBrandItem?.chipList?: ArrayList() for ((index, chip) in chipList.withIndex()) { chip.selected = if (index == chipIndex) 1 else 0 chipList[index] = chip } chipList[chipIndex].isApiCallDone = brandDataList?.size ?: 0 > 0 chipList[chipIndex].dataList = brandDataList shopByBrandItem.chipList = chipList newHomeDataList?.set(position, shopByBrandItem) newHomeDataList?.let { newHomeDataModelLiveData.postValue(it) }



```But im Receiving **com.airbnb.epoxy.ImmutableModelException**, and the twist is if i convert data class to normal class, the exception vanishes and code works fine.
class NewHomeDataModel {
        var viewType: Int = -1
        var displayTitle: String? = null
        var dataList: ArrayList<NewHomeDataItem>? = null
        var chipList: ArrayList<NewHomeChipBucket>? = null
        var isApiCallDone: Boolean = false
}
After converting data class to normal class epoxy exception vanishes and code works fine.
So whats the issue  in using data class ?
elihart commented 2 years ago

you cannot update a mutable list in your class, whether its a data class or not. changing the list updates the list reference that is already bound to your model

sulemankhan4 commented 2 years ago

you cannot update a mutable list in your class, whether its a data class or not. changing the list updates the list reference that is already bound to your model

but im creating a copy using gson, and why exception arises when using data class and not in using normal class