如何解决iOS 14.5 和 14.6 中的错误,代码在 14.2 中运行良好......如何开始解决这个问题?
总结:
应用在 iOS 14.2 上运行良好;在 iOS 14.5 和 14.6 中,无论是模拟器还是设备,始终崩溃并显示无用的错误消息。我找到了其存在/不存在产生或消除错误的代码,但它是用户界面的重要组成部分。 从哪里开始解决这种情况?
详情:
大型应用程序,全部使用 Swift,几乎所有 UI 都使用 SwiftUI,大量屏幕元素、核心数据、一些多线程。在使用 iOS 14.2 的模拟器中,该应用程序运行良好(就像在我的 iPad 上一样,直到我更新了 iOS)。我最近将我的 iPad 更新到了 iOS 14.6,当我在设备上运行该应用程序时,我将在下面描述崩溃。 14.2 模拟器中完全相同的代码,运行良好,没有崩溃。当我在 iOS 14.5 模拟器(14.6 还没有可用的 sim)上运行代码时,该模拟器和设备上的崩溃相同。
缩小崩溃发生的范围,我发现它的代码如下所示:
var body: some View {
GeometryReader { geometry in
HStack(spacing: 0){
LeftMainColumn()
CenterMainColumn()
RightMainColumn()
}
}
}
每一列看起来像这样:
struct LeftMainColumn: View {
func someData() -> [String] {
return ["a","b","c"] // different data in each column
}
var body: some View {
GeometryReader { geom in
ZStack {
Rectangle()
.fill(Color.red)
List{
ForEach(someData(),id: \.self) { dat in
Text("data: \(dat)")
.listRowBackground(Color.red)
}
}
} // closes ZStack
} // closes GeometryReader
} // closes var body
} // closes struct
另外两列基本相同,只是数据不同。
与实际项目相比,此代码高度简化,我包含它是为了传达在哪里 崩溃正在发生。使用这个简单的代码,不会发生崩溃。 (实际应用中的屏幕上还有更多内容;也许这强调了足以产生问题的内容。)
当我注释掉任何一列时,其余两列在 14.5 sim 和 14.6 设备上显示正常。当我拥有所有 3 列时,14.5 sim 和 14.6 设备崩溃,并显示以下报告(14.2 sim 在所有 3 列中都运行良好):
2021-06-24 19:58:02.989478-0500 AppName[40446:97339181] *** Assertion failure in -[_TtC7SwiftUIP33_BFB370BA5F1BADDC9D83021565761A4925UpdateCoalescingTableView _endCellAnimationsWithContext:],UITableView.m:2193
2021-06-24 19:58:03.020894-0500 AppName[40446:97339181] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException',reason: 'attempt to insert section 0 but there are only 0 sections after the update'
*** First throw call stack:
(
0 CoreFoundation 0x000000010daa7fba __exceptionPreprocess + 242
1 libobjc.A.dylib 0x000000010d0aaff5 objc_exception_throw + 48
2 CoreFoundation 0x000000010daa7de3 +[NSException raise:format:] + 0
3 Foundation 0x000000010cb478e7 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 UIKitCore 0x000000012bf82920 -[UITableView _endCellAnimationsWithContext:] + 9394
5 UIKitCore 0x000000012bf9dff7 -[UITableView endUpdatesWithContext:] + 116
6 UIKitCore 0x000000012bf9e1c9 -[UITableView _performBatchUpdates:withContext:completion:] + 253
7 UIKitCore 0x000000012bf9e2af -[UITableView performBatchUpdates:completion:] + 97
8 SwiftUI 0x000000010f37a801 $s7SwiftUI25UpdateCoalescingTableView33_BFB370BA5F1BADDC9D83021565761A49LLC19performBatchUpdates_10completionyyycSg_ySbcSgtF + 378
9 SwiftUI 0x000000010f37a8da $s7SwiftUI25UpdateCoalescingTableView33_BFB370BA5F1BADDC9D83021565761A49LLC19performBatchUpdates_10completionyyycSg_ySbcSgtFTo + 175
10 SwiftUI 0x000000010f37d31d $s7SwiftUI19ListCoreCoordinator33_BFB370BA5F1BADDC9D83021565761A49LLC17updateUITableView_2to11transactionySo0mN0C_xAA11TransactionVtF14performUpdatesL_yyAA0cD10DataSourceRzAA16SelectionManagerR_0V5ValueQy_AORtzr0_lF + 1594
11 SwiftUI 0x000000010f723652 $sIg_Ieg_TR + 12
12 SwiftUI 0x000000010f723668 $sIeg_IyB_TR + 14
13 UIKitCore 0x000000012c2cfbb9 +[UIView(Animation) performWithoutAnimation:] + 84
14 SwiftUI 0x000000010f37dc6f $s7SwiftUI19ListCoreCoordinator33_BFB370BA5F1BADDC9D83021565761A49LLC17updateUITableView_2to11transactionySo0mN0C_xAA11TransactionVtFyycfU_ + 467
15 SwiftUI 0x000000010f266eb7 $sIeg_ytIegr_TR + 12
16 SwiftUI 0x000000010f3851cf $sIeg_ytIegr_TRTA + 17
17 SwiftUI 0x000000010f38899f $sIeg_ytIegr_TRTA.42 + 9
18 SwiftUI 0x000000010f250ec4 $s7SwiftUI6UpdateO3endyyFZ + 433
19 SwiftUI 0x000000010f0a83fd $sSo9NSRunLoopC7SwiftUIE14flushObserversyyFZ + 148
20 SwiftUI 0x000000010f0a8367 $sSo9NSRunLoopC7SwiftUIE11addobserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_yyXEfU_ + 9
21 SwiftUI 0x000000010f0a35fe $ss5Error_pIgzo_ytsAA_pIegrzo_TR95$sSo9NSRunLoopC7SwiftUIE11addobserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_yyXEfU_Tf3npf_n + 14
22 libswiftObjectiveC.dylib 0x000000011445e03e $s10ObjectiveC15autoreleasepool8invokingxxyKXE_tKlF + 46
23 SwiftUI 0x000000010f0a8356 $sSo9NSRunLoopC7SwiftUIE11addobserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_ + 35
24 SwiftUI 0x000000010f0a8459 $sSo9NSRunLoopC7SwiftUIE11addobserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_To + 43
25 CoreFoundation 0x000000010da14d31 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
26 CoreFoundation 0x000000010da0f542 __CFRunLoopDoObservers + 541
27 CoreFoundation 0x000000010da0faf5 __CFRunLoopRun + 1129
28 CoreFoundation 0x000000010da0f1a7 CFRunLoopRunSpecific + 567
29 GraphicsServices 0x00000001194d2d85 GSEventRunModal + 139
30 UIKitCore 0x000000012bd854df -[UIApplication _run] + 912
31 UIKitCore 0x000000012bd8a39c UIApplicationMain + 101
32 PlayingWithSwiftUI_10_OverallFlow 0x000000010a18ac6b main + 75
33 libdyld.dylib 0x0000000115fa0bbd start + 1
34 ??? 0x0000000000000001 0x0 + 1
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',reason: 'attempt to insert section 0 but there are only 0 sections after the update'
terminating with uncaught exception of type NSException
CoreSimulator 757.5 - Device: 14.5 iPadAir3rdGen (7EB4A57C-0A14-4C84-8740-943D694B877A) - Runtime: iOS 14.5 (18E182) - DeviceType: iPad Air (3rd generation)
我还可以通过将 ForEach
语句替换为一系列 Text("x")
语句来消除崩溃,但该应用程序的全部意义在于它显示的是实际数据,而不是 {{1} }.
好的,我已经确定了哪里崩溃发生在 14.5 / 14.6 sim/device 中,但我不知道如何去做解决崩溃。该应用程序绝对必须存在所有 3 列,因此“只删除一列”虽然它绝对避免了错误,但不是解决方案。
除了“希望他们在 iOS 15 中解决这个问题”之外,人们从哪里开始着手修复在 iOS 14.2 中运行(仍然运行)良好但在更高的 iOS 版本中消失的应用程序?
解决方法
你提到了核心数据。
我刚刚在我的项目中找到了解决此问题的方法,该项目遇到了同样的问题:
- 从 14.5 开始新崩溃
- 无用的堆栈跟踪
- 日志输出抱怨
Error in sample.int(length(x),size,replace,prob) : invalid first argument
对于这种结构的文件:
attempt to insert section 0 but there are only 0 sections after the update
修复方法是检查您的提取内容的计数是否大于 0:
struct CrashingView: View {
@FetchRequest var listContent: FetchedResults<Entity>
var body: some View {
List {
ForEach(listContent,id:\.self) {
Cell($0)
}
}
}
有点愚蠢,是的,但很容易解决。希望对您有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。