问题描述
我是 Realm 的新手,正在尝试使用 UICollectionViewDiffableDataSource
实现 Realm。但是,我发现在插入/删除新对象后很难更新数据源。我尝试使用 NotificationToken
进行更新,但它崩溃了。这是我迄今为止尝试过的代码:
fileprivate let realm = try! Realm()
fileprivate var notificationToken: NotificationToken?
fileprivate var items: Results<Item>?
fileprivate enum Section: String,CaseIterable,Hashable {
case main
}
fileprivate typealias DataSource = UICollectionViewDiffableDataSource<Section,Item>
fileprivate typealias Snapshot = NSDiffableDataSourceSnapshot<Section,Item>
fileprivate lazy var dataSource = itemsDataSource()
fileprivate func itemsDataSource() -> DataSource {
let dataSource = DataSource(collectionView: itemsCollectionView) { collectionView,indexPath,item in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId",for: indexPath) as! STItemCell
cell.titleLabel.text = item.name
return cell
}
return dataSource
}
fileprivate func bind() {
items = realm.objects(Item.self).sorted(byKeyPath: "order").filter("isDeleted == false")
notificationToken = items?.observe({ [weak self] changes in
guard let datasource = self?.datasource else { return }
switch changes {
case .initial:
self?.applySnapshot()
case .update(_,deletions: let deletions,insertions: let insertions,modifications: let modifications):
var snapshot = datasource.snapshot()
if !deletions.isEmpty {
let itemIdentifiers = deletions.compactMap { item in
return datasource.itemIdentifier(for: IndexPath(item: item,section: 0))
}
snapshot.deleteItems(itemIdentifiers)
}
if !insertions.isEmpty {
let itemIdentifiers = insertions.compactMap { item in
return datasource.itemIdentifier(for: IndexPath(item: item,section: 0))
}
// snapshot.insertItems(...) // Don't kNow how to implement this...
}
if !modifications.isEmpty {
let itemIdentifiers = modifications.compactMap { item in
return datasource.itemIdentifier(for: IndexPath(item: item,section: 0))
}
snapshot.reloadItems(itemIdentifiers)
}
datasource.apply(snapshot,animatingDifferences: true)
case .error(let error):
fatalError("\(error)")
}
})
}
fileprivate func applySnapshot(animatingDifferences: Bool = true) {
var snapshot = Snapshot()
snapshot.appendSections([.main])
guard let items = items else { return }
snapshot.appendItems(Array(items))
datasource.apply(snapshot,animatingDifferences: animatingDifferences)
}
非常感谢您在这方面的帮助。谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)