插入 UICollectionViewCell 时在 iOS 10.3.1 上崩溃
Posted
技术标签:
【中文标题】插入 UICollectionViewCell 时在 iOS 10.3.1 上崩溃【英文标题】:Crash on iOS 10.3.1 when inserting a UICollectionViewCell 【发布时间】:2018-03-26 10:14:30 【问题描述】:我正在从不同的应用程序中移动一些内容,这样做需要我移动一个 NIB。移动此 NIB 会导致 ios 10 崩溃,每次都发生这种崩溃 - 此崩溃没有关于堆栈溢出的搜索结果或许多其他在线信息。
我正在使用此代码重新加载单元格:
collectionView?.performBatchUpdates(
collectionView?.insertItems(at: [ indexPath ])
, completion: nil)
具体来说,当插入集合视图单元格时,这是错误的:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
frame #0: 0x0000000105015acb libobjc.A.dylib`objc_msgSend + 11
frame #1: 0x0000000104c5506f Foundation`-[NSLayoutAnchor nsli_lowerIntoExpression:withCoefficient:forConstraint:] + 376
frame #2: 0x0000000104b21c11 Foundation`-[NSLayoutConstraint _lowerIntoExpression:reportingConstantIsRounded:] + 99
frame #3: 0x0000000104b1b56e Foundation`-[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 121
frame #4: 0x000000010721fc71 UIKit`__57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 454
frame #5: 0x0000000104b2182b Foundation`-[NSISEngine withBehaviors:performModifications:] + 155
frame #6: 0x000000010721fa84 UIKit`__57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 604
frame #7: 0x000000010721f800 UIKit`-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 223
frame #8: 0x000000010721e933 UIKit`-[UIView(AdditionalLayoutSupport) _initializeHostedLayoutEngine] + 447
frame #9: 0x0000000107211499 UIKit`-[UIView(UIConstraintBasedLayout) _layoutEngine_windowDidChange] + 130
frame #10: 0x0000000106917909 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 209
frame #11: 0x000000010690ab96 UIKit`__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 151
frame #12: 0x000000010690aa7d UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 828
frame #13: 0x000000010691aa0a UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 1927
frame #14: 0x000000010718aef7 UIKit`-[UICollectionView _addControlledSubview:atZIndex:] + 571
frame #15: 0x000000010718dec0 UIKit`-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 1881
frame #16: 0x000000010718d761 UIKit`-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 35
frame #17: 0x00000001071ac194 UIKit`__51-[UICollectionView _viewAnimationsForCurrentUpdate]_block_invoke.1941 + 564
frame #18: 0x00000001071a95bb UIKit`-[UICollectionView _viewAnimationsForCurrentUpdate] + 5141
frame #19: 0x00000001071aebf3 UIKit`__71-[UICollectionView _updateWithItems:tentativelyForReordering:animator:]_block_invoke.2012 + 197
frame #20: 0x000000010691308e UIKit`+[UIView(Animation) performWithoutAnimation:] + 90
frame #21: 0x00000001071ad82d UIKit`-[UICollectionView _updateWithItems:tentativelyForReordering:animator:] + 3856
frame #22: 0x00000001071a7b33 UIKit`-[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 17030
frame #23: 0x00000001071b01cd UIKit`-[UICollectionView _endUpdatesWithInvalidationContext:tentativelyForReordering:animator:] + 71
frame #24: 0x00000001071b0514 UIKit`-[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:] + 437
frame #25: 0x00000001071b033c UIKit`-[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 91
frame #26: 0x00000001071b02be UIKit`-[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 74
frame #27: 0x00000001071b0213 UIKit`-[UICollectionView performBatchUpdates:completion:] + 53
* frame #28: 0x00000001046f44be IndexListSampleApp`closure #1 in RemoteIndexListContent.willDisplay(result=success, self=0x00006080000d0fb0, collectionView=0x00007f924588e600, indexPath=2 indices) at RemoteIndexListContent.swift:98
frame #29: 0x00000001046f464d IndexListSampleApp`partial apply for closure #1 in RemoteIndexListContent.willDisplay(itemAtIndexPath:cell:collectionView:) at RemoteIndexListContent.swift:0
frame #30: 0x000000010458f528 IndexListSampleApp`specialized closure #1 in RemoteLiveEventTileContentProvider.loadContent(fixtures=<unavailable>, self=<unavailable>, completion=0x00000001046f45d0 IndexListSampleApp`partial apply forwarder for closure #1 (Result.Result<IndexListFoundation.IndexListContent>) -> () in IndexListFoundation.RemoteIndexListContent.willDisplay(itemAtIndexPath: Foundation.IndexPath, cell: __ObjC.UICollectionViewCell, collectionView: __ObjC.UICollectionView) -> () at RemoteIndexListContent.swift) at RemoteLiveEventTileContentProvider.swift:49 [opt]
frame #31: 0x000000010458cc0b IndexListSampleApp`partial apply for closure #1 in RemoteLiveEventTileContentProvider.loadContent(completion:) [inlined] closure #1 (Swift.Optional<Swift.Array<Core.Fixture>>, Swift.Optional<Swift.Error>) -> () in LiveEvent.RemoteLiveEventTileContentProvider.loadContent(completion: (Result.Result<IndexListFoundation.IndexListContent>) -> ()) -> () at RemoteLiveEventTileContentProvider.swift:0 [opt]
这只发生在 iOS 10.2.1 -> 10.3.1 - 在 iOS 9 和 iOS 11 上正常。我使用的是 Xcode 9.0.1,崩溃也可以在 Xcode 9.2 上重现。
【问题讨论】:
显示一些代码。 @Skywalker 这是一个 NIB 问题,我可以显示我用来重新加载的代码,但它是相当普通的 UICollectionView Nvm 如果你已经修复了 @Skywalker 我添加了一个代码示例,我想确保没有人和我遇到同样的问题!这很烦人,而且不太容易调试 也被视为"[UILayoutGuide nsli_lowerIntoExpression:withCoefficient:forConstraint:]"
【参考方案1】:
修复非常简单。我勾选了"Use Safe Area Layout Guides"
- 这显然在 iOS 10.2.1 -> 10.3.1 上存在潜在问题。取消勾选此选项可解决崩溃问题!
【讨论】:
以上是关于插入 UICollectionViewCell 时在 iOS 10.3.1 上崩溃的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionViewCell 在帧更新时不会改变
滚动 UICollectionView 时保留 UICollectionViewCell
UICollectionViewCell 滚动时缩放单元格大小
iOS bug记录UICollectionviewCell刷新变得这么莫名其妙?