首页 > 解决方案 > 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"),但应用程序的重点在于它显示的是实际数据,而不是x x x.

好的,所以我已经确定了 14.5 / 14.6 sim/device 中发生崩溃的位置,但我不知道如何解决崩溃。该应用程序绝对必须存在所有 3 列,因此“只删除一列”虽然它绝对避免了错误,但并不是解决方案。

除了“希望他们在 iOS 15 中解决这个问题”之外,人们从哪里开始着手修复在 iOS 14.2 中运行(仍然运行)正常但在更高版本的 iOS 版本中死掉的应用程序?

标签: iosswiftuicrashswiftui-listios14.5

解决方案


你提到了核心数据。

我刚刚在我的项目中找到了解决这个问题的方法,它遇到了同样的问题:

  • 从 14.5 开始新崩溃
  • 无用的堆栈跟踪
  • 日志输出抱怨attempt to insert section 0 but there are only 0 sections after the update

对于这种结构的文件:

struct CrashingView: View {
    
    @FetchRequest var listContent: FetchedResults<Entity>

    var body: some View {
        List {
            ForEach(listContent, id:\.self) {
                 Cell($0)
        }
    }

}

解决方法是检查您的获取内容的计数是否大于 0:

struct CrashingView: View {
    
    @FetchRequest var listContent: FetchedResults<Entity>

    var body: some View {

        if listContent.count > 0 {
            List {
                ForEach(listContent, id:\.self) {
                     Cell($0)
                 }
            }
        } else {
          EmptyView()
        }
    }

}

有点愚蠢,是的,但很容易解决。希望它对您的情况有所帮助。


推荐阅读