原因不明的崩溃:NSInvalidArgumentException,同一个视图控制器实例被推了不止一次



我用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,我不完全确定我怎么能做到这一点。

任何指示将不胜感激!

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

使用pushViewController:transition:forceImmediate不是一个好主意,因为pushViewController:animated将调用这个未记录的方法。你可以使用其他文档化的方法来改变动画,而不是以这种方式改变过渡。这条线可能对你有帮助。您还需要在pushViewController:animated中添加保护,还需要设置transition.duration。反复调用pushViewController可能会导致应用程序崩溃。

当你把你的navigationLink放在一个有观察数据的List中时,就会出现这个问题。

如何修复:

将NavigationLink从列表内部移除,放到列表外部。

相关内容

  • 没有找到相关文章

最新更新