Handling “Requested the number of rows for section (-1)” Error with RxDataSources’s RxTableViewSectionedReloadDataSource

I am developing an iOS application using Swift and RxSwift, specifically utilizing RxTableViewSectionedReloadDataSource for managing my UITableView data bindings. I am not using DataSourceDelegate. Instead, I manage data reactively. Occasionally, when my data source temporarily becomes empty, I encounter a crash with the following error message:

Assertion failure in NSInteger
_UITableViewRowDataNumberOfRowsInSection(UITableViewRowData *__unsafe_unretained, NSInteger)(), 
UITableViewRowData.m:1761
*** Terminating app due to uncaught exception:
'NSInternalInconsistencyException', reason: 'Requested the number of rows for section (-1) which is out of bounds.'

This error appears to happen when I try to refresh or scroll the UITableView. How can I handle or prevent this exception from occurring when my section list is empty or when there are rapid updates to the data source?

I have implemented a reactive data binding using RxSwift’s RxTableViewSectionedReloadDataSource to manage data in a UITableView. Here’s how I bind the data to the table view:

fileprivate func bindViewModel() {
    viewModel?.sectionList
        .debug("Section List Binding")
        .observe(on: MainScheduler.asyncInstance)
        .filter { !$0.isEmpty }
        .catch { error in
            print("Error in binding section list: (error)")
            return .empty()
        }
        .bind(to: tableView.rx.items(dataSource: temporaryExtraDataSource()))
        .disposed(by: disposeBag)
}

Additionally, I have added safety checks in the data source to handle cases when the index path might be out of the valid range, especially when the sectionList is empty, but the return block isn’t even executing.

func temporaryExtraDataSource() -> RxTableViewSectionedReloadDataSource<TemporaryExtraSectionModel> {
    return RxTableViewSectionedReloadDataSource<TemporaryExtraSectionModel>(
        configureCell: { dataSource, tableView, indexPath, _ in
            guard indexPath.section >= 0, indexPath.section < dataSource.sectionModels.count,
                  indexPath.row >= 0, indexPath.row < dataSource.sectionModels[indexPath.section].items.count else {
                return UITableViewCell() // Safe fallback
            }
            
            switch dataSource[indexPath] {
            case let .contentListSectionItem(control):
                let cell = tableView.cellWithIdentifier(cell: TemporaryExtraContentCell.self)
                let contents = Utils.instance.getContentData(control: control)
                cell.delegate = self
                cell.data.accept(contents)
                return cell
            }
        }
    )
}

I was expecting the table view to handle empty sections gracefully, preventing any crashes when the data source becomes temporarily empty or when the sections update rapidly. However, I still encounter a crash when the sections list is empty and an interaction (like a scroll) occurs. The crash points to an ‘out of bounds’ error when requesting the number of rows for a section.

1

Based on the information provided, I think your problem is with the .filter { !$0.isEmpty } line.

If there are no items, you need to let the table view know, otherwise it won’t update properly.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật