Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immutable Exception in Data class, Normal Class #1247

Open
sulemankhan4 opened this issue Nov 5, 2021 · 2 comments
Open

Immutable Exception in Data class, Normal Class #1247

sulemankhan4 opened this issue Nov 5, 2021 · 2 comments

Comments

@sulemankhan4
Copy link

sulemankhan4 commented Nov 5, 2021

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<NewHomeDataModel>(
                        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)
                }
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
Copy link
Contributor

elihart commented Nov 5, 2021

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants