- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个从 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/
我有一个并排有两列的布局。有没有一种简单的方法可以使用单个 UICollectionView 来做到这一点?唯一的要求是该解决方案必须适用于 iOS 8,并且单元格必须在每列中垂直堆叠,如下所示:
我使用标准 UICollectionView与部分。我的单元格像网格一样排列。如果用户使用 Apple TV Remote 移动焦点,则所选方向的下一个单元格会正确聚焦。但是如果网格中有“间隙”,默认
这是我的 ImageController 类(一个 ViewController)仅显示 Collection View 的背景,而其中的单元格则不显示。有什么帮助吗?是不是我没有正确初始化什么? 这
我希望能够在 UICollectionView 中设置内容大小的最小高度,这样我就可以隐藏/显示 UISearchbar,类似于在 iBooks 上完成的方式。 但是,我不想对布局进行子类化,因为我想
有没有办法禁用 UICollectionView 的自动滚动细胞何时聚焦?当单元格进入焦点时,我想手动调整单元格的内容偏移量。 我不想更新内容偏移量: - (void)didUpdateFocusIn
如何将 UICollectionView 中的所有单元格垂直和水平居中使用 UICollectionViewFlowLayout ? 流布局根据滚动方向在右侧或底部留下间距。我想在所有方面设置相同的填
所以这是一个很无聊的问题,但我只是想不出一种非常简单的方法来检测当前关注的项目的 indexPath . 我环顾四周,希望看到一些非常简单的东西,例如 collectionView.indexPath
每当 UICollectionView 完全加载时,我都必须执行一些操作,即此时应调用所有 UICollectionView 的数据源/布局方法。我怎么知道?是否有任何委托(delegate)方法可以
每当 UICollectionView 完全加载时,我都必须执行一些操作,即此时应调用所有 UICollectionView 的数据源/布局方法。我怎么知道?是否有任何委托(delegate)方法可以
所以,UICollectionView 现在对我来说真的很痛苦。假设我有一个 UIViewController ,其中嵌入了一个 UICollectionView 。 CollectionView 的
好的,如果我有 UIViewController如果我使用这个 `preferredFocusedView ,我可以制作一个专注于 tvOS 的 View ,但是如何让 UICollectionVie
我已经构建了 2 个 Collection View ,1 个水平滚动(在顶部)和 1 个垂直滚动(在中间 - 底部),用于在我的 Objective-C iOS 应用程序中查看 2 组不同的内容,类
这就是我的问题,您在 gif 上看到的 4 个橙色矩形是单个垂直 UICollectionView = OrangeCollectionView。 绿色和紫色“卡片” View 是另一个 UIColl
大家好,我是 IOS 开发的新手,我有一个 UICollection,我在其中使用 SDWebImageCache 加载图像。问题是我的 UICollectionview 单元格在快速滚动时感觉像抽搐
我在 ViewController 中有两个 Collection View 。需要在用户滚动底部 UICollectionView 时自动滚动顶部 collectionView。 最佳答案 你可以试
我正在研究 UICollectionView,我有一个在单元格中使用 collectionView 的想法?可以吗 当我运行项目时,我收到一个错误 "InterFace Builder StoryBo
大家好,我收到以下错误 -UICollectionView 必须使用非 nil 布局参数进行初始化 PopularShotsCollectionViewController 的代码: import U
我的应用程序出现此错误: *** -[UICollectionView _endItemAnimations] 断言失败,/SourceCache/UIKit/UIKit-2372/UICollect
当我的 UIViewController 与 UICollectionView 一起出现时,内容会向上滚动一点,出现时。 我实现了 scrollViewDidScroll:我正在记录 contentO
我遇到了一个问题,在我的 UICollectionView 中,当返回的记录为 0 时,我需要显示一个页脚。下面的代码似乎运行良好,当没有记录时,显示页脚。但是,有一个问题,当有记录时,虽然foote
我是一名优秀的程序员,十分优秀!