【问题标题】:Unexplained crash: NSInvalidArgumentException, same view controller instance pushed more than once无法解释的崩溃:NSInvalidArgumentException,多次推送同一个视图控制器实例
【发布时间】:2021-06-03 13:02:57
【问题描述】:

我在使用 SwiftUI 构建的 iOS 应用上看到了无法解释的崩溃。它只影响一些用户,我无法在本地重现。

这是堆栈跟踪:

Fatal Exception: NSInvalidArgumentException

<_TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS_19SidebarStyleContext_: 0x110859800>
is pushing the same view controller instance(<_TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS_14NoStyleContext_: 0x11088a400>)
more than once which is not supported and is most likely an error in the application : `com.myapp`


Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x1893a186c __exceptionPreprocess
1  libobjc.A.dylib                0x19e3bcc50 objc_exception_throw
2  UIKitCore                      0x18b575514 -[UINavigationController pushViewController:transition:forceImmediate:]
3  UIKitCore                      0x18b5751f4 -[UINavigationController pushViewController:animated:]
4  UIKitCore                      0x18b6047c8 __45-[UISplitViewControllerPanelImpl showColumn:]_block_invoke
5  UIKitCore                      0x18b57eda8 -[UINavigationController _executeSplitViewControllerActions:]
6  UIKitCore                      0x18b6045bc -[UISplitViewControllerPanelImpl showColumn:]
7  UIKitCore                      0x18b5e4c54 -[UISplitViewController showColumn:]
8  SwiftUI                        0x1903784f4 closure #1 in closure #1 in NavigationBridge_PhoneTV.push(_:onto:animated:)
9  SwiftUI                        0x19042728c thunk for @escaping @callee_guaranteed () -> ()
10 UIKitCore                      0x18c23f544 -[_UIAfterCACommitBlock run]
11 UIKitCore                      0x18bd6700c _runAfterCACommitDeferredBlocks
12 UIKitCore                      0x18bd559a0 _cleanUpAfterCAFlushAndRunDeferredBlocks
13 UIKitCore                      0x18bd89bb4 _afterCACommitHandler
14 CoreFoundation                 0x18931c358 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
15 CoreFoundation                 0x1893165c4 __CFRunLoopDoObservers
16 CoreFoundation                 0x189316b74 __CFRunLoopRun
17 CoreFoundation                 0x18931621c CFRunLoopRunSpecific
18 GraphicsServices               0x1a0ee2784 GSEventRunModal
19 UIKitCore                      0x18bd56ee8 -[UIApplication _run]
20 UIKitCore                      0x18bd5c75c UIApplicationMain
21 MYAPP                          0x100dbecc0 main + 7 (PushupsMode.swift:7)
22 libdyld.dylib                  0x188fd66b0 start

PushupsMode.swift:7好像完全不相关:

import Foundation

extension WorkoutModel {
    mutating func startPushups() {
        var pushupsCount = 10 + p.intensityOptionPicked * 10
        
        if(round > 6 || round < 3) { // <<--- this is the line crashing
            pushupsCount = Int(pushupsCount / 2)
        }

        // More code after that point
    }
}

round 变量是在另一个文件中定义的,但它周围并没有什么真正重要的东西:

import Foundation
import SwiftUI

struct WorkoutModel: Identifiable {
    // bunch of stuff before
    var round: Int = -1
    // bunch of stuff after
} 

显然查看错误消息,似乎有些东西被推送了两次......但是由于我使用的是 SwiftUI,我不完全确定我是如何做到这一点的。

任何指针将不胜感激!

【问题讨论】:

  • 我很好奇round 的声明位置。由于它似乎在一条看似无害的线上崩溃,我会调查为什么这条线特别是一个问题。也许多个控制器存在线程问题?虽然我不知道为什么会有多次推送。
  • 是否有您订阅(等待)某些内容的业务逻辑,然后基于您正在导航的内容。所以此代码块可能会收到超出需要的通知?
  • @user212514 我已经更新了问题,但整个文件与推送视图无关
  • @EnesKaraosman if(round &gt; 6 || round &lt; 3) 实际上甚至不应该在那个时候被调用,如果我正在关注 crashlytics 中的内容。这是一种“模式”,不用于导致崩溃的情况。
  • 您能否在崩溃日志中看到它是否与特定的 iOS 版本相关?

标签: ios swift swiftui


【解决方案1】:

您的问题可能是您在PushupsMode.swift:8 上将pushupsCount 定义为Int(,这导致:8:7 上的异常预执行失败。尝试将其定义为 pushupsCount = Int pushupsCount / 2

【讨论】:

    【解决方案2】:

    使用pushViewController:transition:forceImmediate 不是一个好主意,因为pushViewController:animated 将调用这个未记录的方法。除了以这种方式更改过渡,您还可以使用其他记录在案的方法来更改动画。这个thread 可能对你有帮助。您还需要在pushViewController:animated 中添加保护,还需要设置transition.duration。一次又一次地调用pushViewController 可能会导致应用崩溃。

    【讨论】:

      【解决方案3】:

      当您将 navigationLink 放置在包含观察数据的列表中时会导致此问题。

      如何解决:

      从列表内部删除 NavigationLink 并将其放在外部。

      【讨论】:

        猜你喜欢
        • 2011-01-03
        • 1970-01-01
        • 2016-05-26
        • 2011-01-21
        • 2011-10-28
        • 1970-01-01
        • 2020-08-30
        • 2019-01-04
        • 1970-01-01
        相关资源
        最近更新 更多