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