简体   繁体   English

当使用NavigationLink,@ EnvironmentObject和List时,Xcode 11 beta 3崩溃

[英]Xcode 11 beta 3 crashing when using NavigationLink, @EnvironmentObject and List together

I've got a strange crash in SwiftUI / Xcode 11 beta 3 with code like the one below (I've kept only the bare minimum to show the behavior): 我在SwiftUI / Xcode 11 beta 3中遇到了一个奇怪的崩溃,代码如下所示(我只保留了最低限度以显示行为):

import SwiftUI
import Combine

final class AppData: BindableObject  {
    let didChange = PassthroughSubject<AppData, Never>()

    init() { }
}

struct ContentView : View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailView() ) {
                Text("link")
            }
        }
    }
}

struct DetailView : View {
    @EnvironmentObject var appData: AppData
//  @ObjectBinding var appData = AppData() -> Works 

    var body: some View {
        List {
            Text("A")
            Text("B")
            Text("C")
        }
    }
}

The BindableObject is injected in SceneDelegate.swift like this: BindableObjectSceneDelegate.swift注入,如下所示:

....
        // Use a UIHostingController as window root view controller
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: ContentView()
                                           .environmentObject(AppData()))
            self.window = window
            window.makeKeyAndVisible()
        }
....

When following the NavigationLink it crashes with 跟随NavigationLink它会崩溃

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

If I remove the List view from the detail view it works OK. 如果我从详细信息视图中删除List视图,它可以正常工作。 The same if I use @ObjectBinding instead (like in the commented line in my code). 如果我使用@ObjectBinding (就像在我的代码中的注释行中)。

The same code used to work in previous betas. 过去在以前的测试版中使用相同的代码。

It's a bug in Xcode 11 beta 3. The old behaviour will likely return. 这是Xcode 11 beta 3中的一个错误。旧的行为可能会回归。

From https://developer.apple.com/tutorials/swiftui/handling-user-input as of July 4th 2019: 来自https://developer.apple.com/tutorials/swiftui/handling-user-input截至2019年7月4日:

Step 4 第4步

In Xcode 11 beta 3, the LandmarkDetail view doesn't automatically access the UserData object in the view hierarchy's environment. 在Xcode 11 beta 3中,LandmarkDetail视图不会自动访问视图层次结构环境中的UserData对象。 The workaround for this is to add the environmentObject(_:) modifier to the LandmarkDetail view. 解决方法是将environmentObject(_ :)修饰符添加到LandmarkDetail视图。

I think that this is by design. 我认为这是设计的。 When you create DetailView(), it is disconnected from the hierarchy, and hence it does not inherit the same environment. 创建DetailView()时,它与层次结构断开连接,因此它不会继承相同的环境。

If you change your ContentView to the following, it won't crash. 如果您将ContentView更改为以下内容,则不会崩溃。 I think I remember having a similar problem with modals: 我想我记得模态有类似的问题:


struct ContentView : View {
    @EnvironmentObject var appData: AppData

    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailView().environmentObject(appData) ) {
                Text("link")
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM