gpt4 book ai didi

ios - 如何使用 Realm 和 UICollectionView 防止 NSInternalInconsistencyException

转载 作者:可可西里 更新时间:2023-11-01 00:58:21 24 4
gpt4 key购买 nike

我有一个从 Realm 填充的 UICollectionView。一些用户,似乎是随机的,得到一个 NSInternalInconsistencyException 声明类似的东西

Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (73) must be equal to the number of items contained in that section before the update (73), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).

我的代码基于 Realm 的集合示例。它选择并过滤一些记录:

self.assets = realm.objects(Asset.self).filter("is_deleted = false")

然后它订阅并处理通知:

self.assetsNotificationToken = self.assets!.addNotificationBlock(){ [weak self] (changes: RealmCollectionChange) in

guard let collectionView = self!.collectionView else { return }

guard let strongSelf = self else { return }

switch changes {

case .Initial:

collectionView.reloadData()

case .Update(let _, let deletions, let insertions, let modifications):

strongSelf.collectionView?.performBatchUpdates({

collectionView.insertItemsAtIndexPaths(insertions.map { NSIndexPath(forRow: $0, inSection: 0) })

collectionView.reloadItemsAtIndexPaths(modifications.map { NSIndexPath(forRow: $0, inSection: 0) })

collectionView.deleteItemsAtIndexPaths(deletions.map { NSIndexPath(forRow: $0, inSection: 0) })

}, completion: nil)

case .Error(let error):

log.error(error.localizedDescription)
break

}

}

计数来自:

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

guard assets != nil else {
return 0;
}

return assets!.count

}

我后来切换到RealmGridController

无法找到崩溃的根源,我切换到RealmGridController .这是一个由 Realm 核心贡献者编写的包,它封装了使用 realm + CollectionViews 所需的所有标准功能。

它似乎有效,然后我开始看到完全相同的崩溃。

Fatal Exception: NSInternalInconsistencyException Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (78) must be equal to the number of items contained in that section before the update (78), plus or minus the number of items inserted or deleted from that section (2 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).

Fatal Exception: NSInternalInconsistencyException
0 CoreFoundation 0x1839dadb0 __exceptionPreprocess
1 libobjc.A.dylib 0x18303ff80 objc_exception_throw
2 CoreFoundation 0x1839dac80 +[NSException raise:format:]
3 Foundation 0x184360154 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4 UIKit 0x18938b00c -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:]
5 UIKit 0x18938e464 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:]
6 UIKit 0x18938e2e0 -[UICollectionView _performBatchUpdates:completion:invalidationContext:]
7 UIKit 0x188d2c2a4 -[UICollectionView performBatchUpdates:completion:]
8 RealmGridController 0x1014a8340 specialized RealmGridController.controllerDidChangeContent(RBQFetchedResultsController) -> () (RealmGridController.swift:316)
9 RealmGridController 0x1014a687c @objc RealmGridController.controllerDidChangeContent(RBQFetchedResultsController) -> () (RealmGridController.swift)
10 RBQFetchedResultsController 0x100ff8edc __112-[RBQFetchedResultsController calculateChangesWithAddedSafeObjects:deletedSafeObjects:changedSafeObjects:realm:]_block_invoke.433 (RBQFetchedResultsController.m:842)
11 libdispatch.dylib 0x1834254bc _dispatch_call_block_and_release

最佳答案

RealmGridController 是一个在 Realm 支持细粒度通知之前编写的库,因此它实现了大量自定义逻辑以获得相同的结果。我强烈建议您恢复到原来的逻辑,因为它已深入集成到 Realm 本身。

要记住的一件重要事情是,Realm 查询结果是事件对象;它们会在每次运行循环迭代时自动更新,以包含此后发生的任何更改。因此,self.assets 的内容应该始终与您的 Collection View 显示的内容直接一对一相关。细粒度通知通知仅用作更新任何过时的 UI 元素的机制,并且在它被调用时,self.assets 已经处于最新状态。

您可能需要提供更多信息,说明您的应用在运行时如何修改 self.assets 的内容。如何添加和删除元素?您的应用中是否存在并发问题?

您需要非常小心,不要进行任何手动 UI 更新,因为与 Collection View 的先前状态和 Realm 假设它需要更新的任何意外差异都会产生这些不一致异常。

关于ios - 如何使用 Realm 和 UICollectionView 防止 NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39730887/

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