- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 UICollectionView 和 UICollectionViewDiffableDataSource 并异步和同步获取数据以填充 View 。应用程序崩溃并显示以下消息:“由于未捕获的异常‘NSInternalInconsistencyException’而终止应用程序,原因:‘无效参数不满足:itemCount’。”
我相信这与同步和异步数据获取的组合有关,因为当我实现其中一个时,应用程序按预期运行。
代码的简单版本如下:
struct Model: Hashable {
let id: UUID
}
class ViewController: UIViewController {
private var collectionView: UICollectionView!
private var dataSource: DataSource?
private var asyncItems: [Model] = []
private var syncItems: [Model] = []
typealias DataSource = UICollectionViewDiffableDataSource<Section, Model>
typealias Snapshot = NSDiffableDataSourceSnapshot<Section, Model>
enum Section {
case async
case sync
}
override func viewDidLoad() {
super.viewDidLoad()
setUpCollectionView()
setUpDataSource()
fetchData()
}
func setUpCollectionView() {
let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: setUpCollectionViewLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "reuseIdentifier")
self.collectionView = collectionView
}
func setUpCollectionViewLayout() -> UICollectionViewLayout {
return UICollectionViewCompositionalLayout { (sectionIndex: Int,
layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(1.0))
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(100))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
return NSCollectionLayoutSection(group: group)
}
}
func setUpDataSource() {
self.dataSource = DataSource(collectionView: collectionView) { (collectionView, indexPath, model) -> UICollectionViewCell? in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reuseIdentifier", for: indexPath)
return cell
}
}
func fetchData() {
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
DispatchQueue.global(qos: .background).async { [weak self] in
defer {
dispatchGroup.leave()
}
self?.asyncItems = [Model(id: UUID())]
}
syncItems = [Model(id: UUID())]
dispatchGroup.notify(queue: .main) { [weak self] in
self?.applySnapshot()
}
}
func applySnapshot() {
var snapshot = Snapshot()
snapshot.appendSections([Section.async])
snapshot.appendItems(asyncItems)
snapshot.appendSections([Section.sync])
snapshot.appendItems(syncItems)
dataSource?.apply(snapshot)
}
}
带有堆栈跟踪的调试器输出:
2020-09-04 16:42:26.859624-0500 DiffableDataSource[81736:1400590] *** Assertion failure in -[_UICollectionLayoutItemSolver _frameForAbsoluteIndex:additionalFrameOffset:interSolutionSpacing:repeatAxis:], /Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3920.31.102/_UICollectionLayoutItemSolver.m:1241
2020-09-04 16:42:26.867684-0500 DiffableDataSource[81736:1400590] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: itemCount'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23e3de6e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff512a19b2 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23e3dbe8 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff258e6bd2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 UIKitCore 0x00007fff48abe254 -[_UICollectionLayoutItemSolver _frameForAbsoluteIndex:additionalFrameOffset:interSolutionSpacing:repeatAxis:] + 429
5 UIKitCore 0x00007fff48ac9fe6 -[_UICollectionLayoutSectionFixedSolver frameForIndex:] + 234
6 UIKitCore 0x00007fff48a986d0 -[_UICollectionCompositionalLayoutSolver layoutAttributesForItemAtIndexPath:] + 355
7 UIKitCore 0x00007fff48b730d1 -[UICollectionViewLayout initialLayoutAttributesForAppearingItemAtIndexPath:] + 267
8 UIKitCore 0x00007fff48b3d59e __51-[UICollectionView _viewAnimationsForCurrentUpdate]_block_invoke.1814 + 105
9 UIKitCore 0x00007fff48b3a360 -[UICollectionView _viewAnimationsForCurrentUpdate] + 3213
10 UIKitCore 0x00007fff48b41617 __71-[UICollectionView _updateWithItems:tentativelyForReordering:animator:]_block_invoke.1887 + 118
11 UIKitCore 0x00007fff4986c650 +[UIView(Animation) performWithoutAnimation:] + 84
12 UIKitCore 0x00007fff48b40484 -[UICollectionView _updateWithItems:tentativelyForReordering:animator:] + 3975
13 UIKitCore 0x00007fff48b39005 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 16761
14 UIKitCore 0x00007fff48b42a83 -[UICollectionView _endUpdatesWithInvalidationContext:tentativelyForReordering:animator:] + 71
15 UIKitCore 0x00007fff48b42de2 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:] + 462
16 UIKitCore 0x00007fff48b42bf1 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 90
17 UIKitCore 0x00007fff48b42b74 -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 74
18 UIKitCore 0x00007fff48b42ac9 -[UICollectionView performBatchUpdates:completion:] + 53
19 UIKitCore 0x00007fff48b523ce -[UICollectionView _performDiffableUpdate:] + 44
20 UIKitCore 0x00007fff48af09ea -[_UIDiffableDataSourceViewUpdater _performUpdateWithCollectionViewUpdateItems:dataSourceSnapshotter:updateHandler:completion:] + 467
21 UIKitCore 0x00007fff48ae9ab6 -[__UIDiffableDataSource _commitNewDataSource:withViewUpdates:completion:] + 246
22 UIKitCore 0x00007fff48ae449e __66-[__UIDiffableDataSource applyDifferencesFromSnapshot:completion:]_block_invoke.154 + 190
23 UIKitCore 0x00007fff48ae472d __66-[__UIDiffableDataSource applyDifferencesFromSnapshot:completion:]_block_invoke.180 + 107
24 libdispatch.dylib 0x00000001071b0e8e _dispatch_client_callout + 8
25 libdispatch.dylib 0x00000001071bfae2 _dispatch_lane_barrier_sync_invoke_and_complete + 132
26 UIKitCore 0x00007fff48ae3fa7 -[__UIDiffableDataSource applyDifferencesFromSnapshot:completion:] + 952
27 UIKitCore 0x00007fff48ae4fb9 -[__UIDiffableDataSource applyDifferencesFromSnapshot:animatingDifferences:completion:] + 71
28 libswiftUIKit.dylib 0x00007fff51ed4af4 $s5UIKit34UICollectionViewDiffableDataSourceC5apply_20animatingDifferences10completionyAA010NSDiffableeF8SnapshotVyxq_G_SbyycSgtFTm + 212
29 DiffableDataSource 0x0000000106f2f087 $s18DiffableDataSource14ViewControllerC13applySnapshotyyF + 823
30 DiffableDataSource 0x0000000106f2ec37 $s18DiffableDataSource14ViewControllerC05fetchB0yyFyycfU0_ + 215
31 DiffableDataSource 0x0000000106f2ea40 $sIeg_IeyB_TR + 48
32 libdispatch.dylib 0x00000001071aff11 _dispatch_call_block_and_release + 12
33 libdispatch.dylib 0x00000001071b0e8e _dispatch_client_callout + 8
34 libdispatch.dylib 0x00000001071bed97 _dispatch_main_queue_callback_4CF + 1149
35 CoreFoundation 0x00007fff23da1869 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
36 CoreFoundation 0x00007fff23d9c3b9 __CFRunLoopRun + 2041
37 CoreFoundation 0x00007fff23d9b8a4 CFRunLoopRunSpecific + 404
38 GraphicsServices 0x00007fff38c05bbe GSEventRunModal + 139
39 UIKitCore 0x00007fff49372964 UIApplicationMain + 1605
40 DiffableDataSource 0x0000000106f30f4b main + 75
41 libdyld.dylib 0x00007fff5211c1fd start + 1
42 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
最佳答案
修复此崩溃:
在设置集合 View 布局组时,明确提供项目计数:
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 1)
关于ios - UICollectionViewDiffableDataSource 崩溃 : Invalid parameter not satisfying: itemCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63748268/
我正在尝试理解 UICollectionViewDiffableDataSource 和 NSDiffableDataSourceSnapshot。 我在下面创建了一个非常粗糙的版本。基本上在加载时它
在使用传统的UICollectionView时,我们经常使用如下代码来处理空状态: func collectionView(_ collectionView: UICollectionView, nu
我创建了以下演示 View Controller 以在最小示例中重现该问题。 在这里,我使用 UICollectionViewDiffableDataSource 将相同数据的快照重复应用到相同的集合
我正在使用 UICollectionViewDiffableDataSource 来填充我的 UICollectionView。通过 REST API 收到项目列表后,我创建了一个新快照并像这样应用它
我采用了新的 UICollectionViewDiffableDataSource。每次删除项目时,我都会应用数据源快照: var snapshot = NSDiffableDataSourceSna
我正在构建一个带有 Collection View 的 View Controller ,使用(有点)新的可区分数据源和可组合的 Collection View 布局。我制作了自己的 Section
我有一个 UICollectionView,其中我使用 UICollectionViewCompositionalLayout 布置了两个部分(一个水平滚动,一个垂直滚动)。我将其与 UICollec
我正在尝试将 iOS 13 中内置的一些新差异类与 Core Data 一起使用。我遇到的问题是 controllerdidChangeContentWith没有按预期工作。它传递给我一个快照引用,它
我正在使用 UICollectionViewDiffableDataSource对于我的 collectionView 的数据源。我的 CollectionView 中有 3 个部分: enum Se
我正在使用 UICollectionViewDiffableDataSource和一个 NSFetchedResultsController填充我的 UICollectionView在我的 UIVie
我正在尝试实现 UICollectionViewDiffableDataSource我的 collectionView .我的代码编译得很好,但是我第一次对它应用快照时一直遇到这个错误,并出现以下错误
我正在使用 UICollectionView 和 UICollectionViewDiffableDataSource 并异步和同步获取数据以填充 View 。应用程序崩溃并显示以下消息:“由于未捕获
我需要使用 moveItem(at:to:) 委托(delegate)方法来移动我的 Collection View 单元格。当我实现数据源委托(delegate)方法时它工作正常,但我需要将我的数据
我是一名优秀的程序员,十分优秀!