When I observe my realm model and bind changes to table view it works. But when I try to add row to the table I have some crash
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 1 into section 0, but there are only 1 rows in section 0 after the update'
Can I do it without using standard delegates methods?
Here is my code snippet
let realm = try! Realm()
let places = realm.objects(Place.self)
Observable.from(places)
.bindTo(tableView.rx.items(cellIdentifier: "PlaceCell", cellType: PlaceCell.self)) { (row, element, cell) in
let viewModel = PlaceCellViewModel(place: element)
cell.setup(viewModel: viewModel)
}
.addDisposableTo(disposeBag)
Observable.changesetFrom(places).subscribe(onNext: { [weak self] result, changes in
if let changes = changes {
self?.tableView.beginUpdates()
let indexes = changes.inserted.map { IndexPath(row: $0, section: 0) }
self?.tableView.insertRows(at: indexes, with: .bottom)
self?.tableView.endUpdates()
} else {
self?.tableView.reloadData()
}
})
.addDisposableTo(disposeBag)