由于没有EnvironmentObject的弹出窗口,SwiftUI Mac OS崩溃 [英] SwiftUI Mac OS crash due to popover without EnvironmentObject
问题描述
我在视图中使用的是PopOver.在这种情况下,我正在对CoreData执行FetchRequest.一切正常.它已连接到我的 Environment(\.managedObjectContext)
.但是,总是当我调用弹出窗口并重新加载该视图时,它就会崩溃.
I am using a PopOver in my view. In that view I am doing a FetchRequest to CoreData. It is working fine. It is connected to my Environment(\.managedObjectContext)
. However, always when I call my popover and reload that view, it is crashing.
我读了另一个堆栈溢出"问题,我必须手动将对象推到该弹出窗口.我正在尝试,但仍然崩溃.我正在用它来调用我的弹出窗口:
I read in another Stack Overflow question, that I have to manually push my objects to that popover. I am trying it and it still crashes. I am using this to call my popover:
Button(action:
{
//Call the PopOver
self.s_showPopover = true
})
{
Image("Link")
}
.padding(.trailing, 16)
.buttonStyle(PlainButtonStyle())
.popover(isPresented: self.$s_showPopover, content: { PersonFormPopover().environment(\.managedObjectContext, NSManagedObjectContext.current).environmentObject(userData) })
我可以正确发送对象吗?仅当我至少一次单击弹出窗口时,才会出现崩溃.如果我不单击它,它不会崩溃.
Am I sending my objects correctly? The crash only appear when I click the popover one time minimum. If I don't click it, it doesn't crashes.
在我的PopOver视图中,我什至没有使用该对象,也没有向CoreData发出任何请求.为什么它仍然崩溃?当我重新加载该视图时,它在解析 FetchRequest
时向我显示 foreach
中的错误.看来他那时什么都没有得到.
In my PopOver view I am not even using that Object, or make any request to CoreData. Why is it still crashing? When I reload that view, it shows me the error in my foreach
when parsing the FetchRequest
. It looks like that he do not get any objects at all then.
这是我的PersonPopOver视图,它只是一个空视图.
This is my PersonPopOver view, it is just an empty view.
struct PersonFormPopover: View {
var body: some View {
Text(/"Hello, World!")
}
}
struct PersonFormPopover_Previews: PreviewProvider {
static var previews: some View {
PersonFormPopover()
}
}
在弹出窗口之后再次加载视图时,出现以下崩溃消息.
I get following crash message when loading my view again after the popup.
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
推荐答案
因此,我终于找到了解决方案.感谢@Asperi的帮助...
So, I finally found a solution. Thanks for the help of @Asperi ...
我在实际视图之前使用的是NavigationView.看来SwiftUI没有将我的 Environment Object
推送到 DetailView
.虽然不确定它是否是一个错误,但是可以通过 NavigationLink
和 PopOver
手动将其修复.
I was using a NavigationView before my actual view. It looks like SwiftUI is not pushing my Environment Object
to the DetailView
. Not sure if it a bug though, however pushing it manually at the NavigationLink
and PopOver
fixed it.
在遇到相同问题时,这可能对其他人有所帮助.
Maybe it is helpful for other, when struggling with the same issue.
我刚刚将 NavigationLink
更改为以下代码:
I just changed my NavigationLink
to the following code:
NavigationLink(destination: VenueView().environment(\.managedObjectContext, NSManagedObjectContext.current), tag: 4, selection: self.$userData.currentView) {
我正在手动添加环境
,它可以正常工作.
There I am adding the environment
manually and it works.
我的弹出窗口代码如下:
My code for the popover looked like this:
.popover(isPresented: self.$s_showPopover, content: { PersonFormPopover().environment(\.managedObjectContext, self.managedObjectContext).environmentObject(self.userData)
按照Asperi的建议,我为我的ManagedObjectContext使用了一个本地变量.
I used a local var for my managedObjectContext as Asperi suggested.
这篇关于由于没有EnvironmentObject的弹出窗口,SwiftUI Mac OS崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!