应用已崩溃,正在执行 CustomActivityOnCrash 的 UncaughtExceptionHandler
com.airbnb.epoxy.ImmutableModelException: The model was changed between being added to the controller and being bound
控制器类
class SortFilterController @Inject constructor(
private val schedulersFacade: SchedulersFacade,
private val generateMapOfCategoryFilters: GenerateMapOfCategoryFilters
) : EpoxyController() {
private val tapCountryRelay: PublishRelay<TopsProductFilter> = PublishRelay.create()
var sortFilterViewState: SortFilterViewState = SortFilterViewState()
set(value) {
field = value
requestModelBuild()
}
var sortFilterType: SortFilterType = SortFilterType.ALL
set(value) {
field = value
requestModelBuild()
}
override fun buildModels() {
sortFilterViewState.let { sortFilterViewState ->
sortFilterViewState.filterTypes?.forEach { topsProductFilter ->
when (SortFilterType.getId(topsProductFilter.attributeCode)) {
SortFilterType.COUNTRY -> {
CountryItemModel_()
.id(UUID.randomUUID().toString())
.tapCountryChipRelay(tapCountryRelay)
.countryFilter(topsProductFilter)
.listOfPreSelectedCountryFilters(sortFilterViewState.listOfCurrentlySelectedCountryItems ?: emptyList())
.addTo(this)
}
}
}
}
}
val bindTapCountryRelay: Observable<TopsProductFilter> = tapCountryRelay.hide()
}
// 模型类
@EpoxyModelClass(layout = R.layout.list_item_country_item)
abstract class CountryItemModel : EpoxyBaseModel() {
@EpoxyAttribute
lateinit var tapCountryChipRelay: PublishRelay<TopsProductFilter>
@EpoxyAttribute
lateinit var countryFilter: TopsProductFilter
@EpoxyAttribute
lateinit var listOfPreSelectedCountryFilters: MutableList<TopsProductFilterItem>
override fun bind(holder: EpoxyBaseViewHolder) {
with(holder.itemView) {
// snippet here
}
}
}
在 DialogFragment oncreate 中,我设置了epoxyRecyclerView。
epoxyRecyclerView.setController(sortFilterController)
epoxyRecyclerView.layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false)
并调用控制器上的设置器并请求模型构建
sortFilterController.sortFilterViewState = sortFilterViewState
sortFilterController.sortFilterType = SortFilterType.ALL
但是,问题是我想用一些新数据更改模型中显示的数据。因此,当用户点击一个国家/地区时,我想再次设置设置器。
private fun onTapClearAll() {
// sortFilterViewState has some new data so I want to set it again for display.
// This calling these resulted in a crash as the models epoxy attributes have changed with this new data
sortFilterController.sortFilterViewState = sortFilterViewState
sortFilterController.sortFilterType = SortFilterType.ALL
}
// Then I tried to do the same with a interceptor but again the app will crash.
private fun onTapClearAll() {
sortFilterController.addInterceptor(object : EpoxyController.Interceptor {
override fun intercept(models: MutableList<EpoxyModel<*>>) {
val countryModel = models[0] as CountryItemModel_
countryModel.listOfPreSelectedCountryFilters(sortFilterViewState.listOfCurrentlySelectedCountryItems)
}
})
sortFilterController.requestModelBuild()
}