RxDataSources icon indicating copy to clipboard operation
RxDataSources copied to clipboard

Crash in TableViewSectionDataSource when showing and hiding a section

Open igorkulman opened this issue 6 years ago • 5 comments

In my project I have a chat detail with a list of messages. I use the RxTableViewSectionedAnimatedDataSource with two sections.

In most cases, only the section with the messages is shown but when the user scrolls to the top of the table view, I set the isLoading property so an additional section at the top is shown with just 1 cell showing a spinner.

let data = Observable.combineLatest(viewModel.messages.asObservable(), viewModel.isLoading.asObservable()) { (messages: [Message], isLoading: Bool) -> [AnimatableLoaderSectionModel] in
        var sections: [AnimatableLoaderSectionModel] = []
        if isLoading {
            sections.append(.LoaderSection)
        }
        sections.append(.ItemsSection(items: messages.map({ .Item(item: $0) })))
        return sections
}

After loading the older messages is done isLoading is set back to false and the new messages appear in viewModel.messages.

This works fine most of the times, but when I scroll to top really fast doing it again and again I get a crash in https://github.com/RxSwiftCommunity/RxDataSources/blob/master/Sources/RxDataSources/TableViewSectionedDataSource.swift#L190.

When I do not append the .LoaderSection when constructing data then it does not happen, ever.

To be more exact, in the method

open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        precondition(indexPath.item < _sectionModels[indexPath.section].items.count)

The indexPath is for example section=1, row=6 but the _sectionModels only have 1 item, resulting in an out fo bound crash basically.

It looks like the data source is trying to update some item and still thinks the .LoaderSection exists (so .LoaderSection is section=0 and the update is for section=1) but the _sectionModels does not contain the .LoaderSection anymore.

igorkulman avatar Feb 07 '19 14:02 igorkulman

I guess I'm going through the same situation, do you have any solution?

RafaelPlantard avatar Mar 04 '19 03:03 RafaelPlantard

I'd gladly try to check this but do you have some kind of reduced example?

mkko avatar Mar 04 '19 08:03 mkko

My issue was with other not disposable sequence, not related with TableViewSectionDataSource, thanks for asking @mkko

RafaelPlantard avatar Mar 17 '19 12:03 RafaelPlantard

@RafaelPlantard, could you share what exactly was the issue on your end? I think I'm facing the same problem now with a similar use case like @igorkulman.

okycelt avatar Jan 21 '20 10:01 okycelt

I have same issue. In my case my tableview continuously append and remove cell using accept data to datasource.

SolitaryWolf avatar Aug 17 '20 11:08 SolitaryWolf