RxSwift + RxRealm + RxCocoa 向 UITableView 插入行
RxSwift + RxRealm + RxCocoa insert rows to UITableView
当我观察我的领域模型并将更改绑定到 table 视图时,它起作用了。但是当我尝试向 table 添加行时,我遇到了一些崩溃
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'
我可以不使用标准委托方法来完成吗?
这是我的代码片段
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: [=10=], section: 0) }
self?.tableView.insertRows(at: indexes, with: .bottom)
self?.tableView.endUpdates()
} else {
self?.tableView.reloadData()
}
})
.addDisposableTo(disposeBag)
目前,您有两个订阅相互竞相更新您的table。
您的第一个订阅使用到您的 table 视图的绑定(基本上每次基础数据发生变化时调用 reloadData()
)
您的第二个订阅也会更新您的 table,但这次它使用细粒度方法插入记录。
因此,当第二个订阅开始时 - 您的第一个订阅已经更新了您的 table 并且您收到崩溃错误消息。
目前 RxRealm 中没有包装器来将细粒度的通知包装在活页夹中(不过您可以在 RxRealm 存储库上创建一个关于它的问题!)
如果您想对 table 行进行动画更改,则必须实施 table 查看数据源方法,如下所示:
https://github.com/RxSwiftCommunity/RxRealm/blob/master/Example/RxRealm/ViewController.swift#L74
更新 #1:我想在这之后的一段时间(以及其他类似问题)补充一点,我启动了一个 RxRealmDataSources
库,它的工作方式与普通的 RxDataSources
库非常相似,但是专门用于适应绑定领域类型。该库负责将 RxRealm observable 绑定到 table 或 iOS 和 macOS 上的集合视图,并使用必要的动画更新它们。
这是 GitHub 存储库:https://github.com/RxSwiftCommunity/RxRealmDataSources
当我观察我的领域模型并将更改绑定到 table 视图时,它起作用了。但是当我尝试向 table 添加行时,我遇到了一些崩溃
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'
我可以不使用标准委托方法来完成吗?
这是我的代码片段
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: [=10=], section: 0) }
self?.tableView.insertRows(at: indexes, with: .bottom)
self?.tableView.endUpdates()
} else {
self?.tableView.reloadData()
}
})
.addDisposableTo(disposeBag)
目前,您有两个订阅相互竞相更新您的table。
您的第一个订阅使用到您的 table 视图的绑定(基本上每次基础数据发生变化时调用
reloadData()
)您的第二个订阅也会更新您的 table,但这次它使用细粒度方法插入记录。
因此,当第二个订阅开始时 - 您的第一个订阅已经更新了您的 table 并且您收到崩溃错误消息。
目前 RxRealm 中没有包装器来将细粒度的通知包装在活页夹中(不过您可以在 RxRealm 存储库上创建一个关于它的问题!)
如果您想对 table 行进行动画更改,则必须实施 table 查看数据源方法,如下所示:
https://github.com/RxSwiftCommunity/RxRealm/blob/master/Example/RxRealm/ViewController.swift#L74
更新 #1:我想在这之后的一段时间(以及其他类似问题)补充一点,我启动了一个 RxRealmDataSources
库,它的工作方式与普通的 RxDataSources
库非常相似,但是专门用于适应绑定领域类型。该库负责将 RxRealm observable 绑定到 table 或 iOS 和 macOS 上的集合视图,并使用必要的动画更新它们。
这是 GitHub 存储库:https://github.com/RxSwiftCommunity/RxRealmDataSources