所以我在我的collectionview上使用带有细粒度通知的Realm。当我选择一个单元格时,我不断收到以下错误:
尝试从更新前仅包含4个项目的第0部分删除项目x
我已经关注了Realm的示例代码,无法找到我做错的事情。我的代码如下:
let producers: Results<Producer> = {
ProducersRealm().getAllProducers()
}()
// receive notification
var token: NotificationToken?
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
registerViewCell()
token = producers.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
guard (self?.collectionView) != nil else { return }
// MARK: - Switch on State
switch changes {
case .initial:
self?.collectionView.reloadData()
break
case .update(_, let deletions, let insertions, let modifications):
self?.collectionView.performBatchUpdates({
self?.collectionView.insertItems(at: insertions.map({ IndexPath(row: $0, section: 0)}))
self?.collectionView.deleteItems(at: deletions.map({ IndexPath(row: $0, section: 0)}))
self?.collectionView.reloadItems(at: modifications.map({ IndexPath(row: $0, section: 0)}))
}, completion: nil)
break
case .error(let error):
print(error.localizedDescription)
break
}
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return producers.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
realmProducer = producers[indexPath.row]
favoriseItemRealm(realmProducer!, textAdded: "addedRetailer", textDeleted: "deletedRetailer", view: self, button: nil)
}
因此,当用户按下一个单元格时,这将触发favoriseItemRealm
,这将确保心脏图像显示或不显示(在某些单元格上应该正常工作,但是会回复上面的内容)其他人的错误,而我没有删除单元格)。
关于我做错什么的任何想法?
更新: favoriseItemRealm的代码
// if object is a Producer
if (object as? Producer) != nil {
if let downcastObject = object as? Producer {
let open = realm.objects(Producer.self).filter("id == %@", downcastObject.id).first
try! realm.write {
// user is offline
if hasInternet() == false {
if open!.favorite != true {
open?.favorite = true
if userLoggedIn() {
// write to dabatase if user is logged in, but offline
SyncFavoriteItemRealm.shared.saveOrUpdate((open?.id)!,favorite: true, type: ContentType.PRODUCER.rawValue)
}
} else {
open?.favorite = false
if userLoggedIn() {
// write to dabatase if user is logged in, but offline
SyncFavoriteItemRealm.shared.saveOrUpdate((open?.id)!,favorite: false, type: ContentType.PRODUCER.rawValue)
}
}
// user is online
} else {
if open!.favorite != true {
open?.favorite = true
if userLoggedIn() {
//sync to backend
}
//GoogleAnalyticsHelper.shared.trackFavoriteItem(downcastObject)
} else {
open?.favorite = false
if userLoggedIn() {
// sync to backend
}
}
}
}
}
}
答案 0 :(得分:0)
我遇到了这样的错误原因:
我有一个带有帖子的viewController A 。帖子是Post领域实体的结果。当点击tableView的单元格时,它会导致viewController B ,其中只显示一个帖子。 A 中的令牌仍然有效。如果我从 B 删除帖子,则会触发带有更改的令牌 A 。我决定调试并崩溃在self.collectionV.reloadData()
行。如果我评论这一行,我看到当 B 中删除帖子并且弹出 A 时,UICollectionView中有一个空项目,这个帖子就在哪里。
有一个领域通知:
realmToken = posts?.observe() { [unowned self] changes in
switch changes {
case .initial(_):
self.collectionV.reloadData()
break
case let .update(_, deletions, insertions, modifications):
self.collectionV.performBatchUpdates({
self.collectionV.reloadItems(at: modifications.map { IndexPath(row: $0, section: 0) })
self.collectionV.insertItems(at: insertions.map { IndexPath(row: $0, section: 0) })
self.collectionV.deleteItems(at: deletions.map { IndexPath(row: $0, section: 0) })
}, completion: { (completed: Bool) in
CRASH >>> self.collectionV.reloadData()
})
break
case let .error(error):
self.show(error: error)
break
}
self.stopLoading()
}
我认为可能是因为UICollectionView的内部行为,但也许有人说清楚了吗?