0

I have two entities in my core data model, Computer and Components. A computer can have multiple components, so the relationship is set as to-many. Computer also has an attribute computerName.

When adding a component I want the selected computer to appear in the add component view. This add component view is written in SwiftUI and I have this code to create the ManagedObjectContext and to fetch the computer object:

    @Environment(\.managedObjectContext) var moc: NSManagedObjectContext
    @FetchRequest(entity: Computer.entity(), sortDescriptors: []) var computerToDisplay: FetchedResults<Computer>

I then run the following ForEach loop which I would expect to it to grab a Computer and place it in the Text field

    ForEach(computerToDisplay, id: \.self) { computer in
        Text(computer.computerName)
    }

With this code, when I attempt to open the add component view I get the following error:

Thread 1: Exception: "executeFetchRequest:error: A fetch request must have an entity."

Now, I'm loading the add component view from a UIKit ViewController. So I'm not sure if something isn't getting passed through to the add component view.

This is how I'm loading the component view:

@IBSegueAction func addComponentSegueAction(_ coder: NSCoder) -> UIViewController? {
    let swiftUIView = UIHostingController(rootView: AddComponentView().environment(\.managedObjectContext, moc!))
    present(swiftUIView, animated: true, completion: nil)
    return UIHostingController(coder: coder, rootView: AddComponentView())
}

Any help would be greatly appreciated, thank you.

3
  • Can you show how you are loading ComponentView in UIKit? Commented May 6, 2020 at 16:06
  • Just added the method I'm using to load the AddComponentView in UIKit. Thank you for your help. When I present the AddComponentView I get this runtime error: Context in environment is not connected to a persistent store coordinator: Not sure if that helps. Commented May 6, 2020 at 17:05
  • Please try my suggestion which I posted as answer! Commented May 6, 2020 at 17:09

1 Answer 1

1

Try this,

@IBSegueAction func addComponentSegueAction(_ coder: NSCoder) -> UIViewController? {
    let swiftUIView = UIHostingController(coder: coder, rootView: AddComponentView().environment(\.managedObjectContext, moc!)
    return swiftUIView
}

Seems, you are creating multiple instances of UIHostingViewController with your AddComponentView() and returning the one which don't have AddComponentView() managedObjectContext in environment.

Thanks!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works until I build/run the app again. When I do that I get the following error executeFetchRequest:error: A fetch request must have an entity. .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.