gpt4 book ai didi

swift - Realm - 尝试从第 0 部分删除项目 x,在更新崩溃之前该部分仅包含 4 个项目

转载 作者:行者123 更新时间:2023-11-30 12:35:41 26 4
gpt4 key购买 nike

所以我在我的collectionview上使用带有细粒度通知的Realm。当我选择一个单元格时,我不断收到以下错误:

attempt to delete item x from section 0 which only contains 4 items before the update

我已经按照 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,这将确保心脏图像出现或不出现(在某些单元格上正常工作,但会返回上面的错误在其他人身上,而我没有删除单元格)。关于我做错了什么有什么想法吗?

更新:favoriteItemRealm 的代码

  // 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
}
}
}
}
}

}

最佳答案

我遇到了这样的错误,这就是原因:

我有一个带有帖子的 viewController A。帖子是具有帖子 Realm 实体的结果。当点击 tableView 的单元格时,它会导致 viewController B ,其中仅显示一篇文章。 A 中的 token 仍然有效。如果我从 B 删除帖子,则会触发带有更改的 token A。我决定调试,崩溃发生在 self.collectionV.reloadData() 行。如果我评论这一行,那么我会看到,当在 B 中删除帖子并弹出到 A 时,UICollectionView 中有一个空项目,该帖子在哪里。

有一个 Realm 通知:

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 内部行为,但也许有人说清楚了?

关于swift - Realm - 尝试从第 0 部分删除项目 x,在更新崩溃之前该部分仅包含 4 个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42901056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com