3

I am trying to use core data within my mac app with SwiftUI using Xcode 11. I have checked "Using Core Data" when creating the project. I also have created the entity (called VisitedCases) and used editor to create NSManagedObject Subclass files. I also have set the Codegen to Manual/none. Here is the code in the generated NSManagedObject files:

VisitedCases+CoreDataProperties.swift

extension VisitedCases {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<VisitedCases> {
        return NSFetchRequest<VisitedCases>(entityName: "VisitedCases")
    }

    @NSManaged public var caseNumber: String

} 

VisitedCases+CoreDataClass.swift

@objc(VisitedCases)
public class VisitedCases: NSManagedObject {

}

I called the @Environment variable and the @FetchRequest in ContentView.swift as:

struct ContentView: View {
    @Environment(\.managedObjectContext) var managedObjectContext

    @FetchRequest(entity: VisitedCases.entity(),
                  sortDescriptors: []
                  ) var orders: FetchedResults<VisitedCases>
//@State vars and the rest of the code
}

However, when I run, the app crashed as soon as launch with the following errors in the output:

2020-02-23 18:36:16.889306+0330 ImageSelector[17874:149503] [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'VisitedCases' so +entity is confused.  Have you loaded your NSManagedObjectModel yet ?
CoreData: error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'VisitedCases' so +entity is confused.  Have you loaded your NSManagedObjectModel yet ?
2020-02-23 18:36:16.889389+0330 ImageSelector[17874:149503] [error] error: +[VisitedCases entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[VisitedCases entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
2020-02-23 18:36:16.921131+0330 ImageSelector[17874:149503] executeFetchRequest:error: A fetch request must have an entity.

I also have a function that saves a string to storage and seems to work just fine:

    func addCaseNumber (caseNo: String) {
        guard caseNo != "" else {return}
        let newCaseNumber = VisitedCases(context: self.managedObjectContext)
        newCaseNumber.caseNumber = caseNo
        do {
         try self.managedObjectContext.save()
         print("Case number saved.")
        } catch {
         print(error.localizedDescription)
         }
    }

What is wrong with my code and what should I do to fix it?

6
  • It looks like you did not define or load your Core Data model. Commented Feb 23, 2020 at 16:42
  • stackoverflow.com/questions/53690712/… Commented Feb 23, 2020 at 16:52
  • @MarcT. I have checked the "Use Core Data" when creating the project so I assume that data object has been injected to the delegate etc. I also have defined the Entity in the data model and verified its name. What did I miss? Commented Feb 23, 2020 at 16:53
  • @CranialDev I have already checked that question. I couldn't switch the Codegen to Category/Extension since it would crash as well with the same error. I also didn't quite get the change from "Product.fetchRequest()" to "NSFetchRequest<Product>(entityName: "Product")" part. Where and how should this change be implemented? Commented Feb 23, 2020 at 17:06
  • That part is built into the wrapper. If you look at the CoreData documentation for FetchedResultsController you can see how it all comes together. Are you manually creating your CoreData stack? Commented Feb 23, 2020 at 17:12

3 Answers 3

1

Try assigning your managedObjectContext in your ContentView like this

let managedObjectContext: NSManagedObjectContext =  ((UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext)!

If this works it means that your @Environment var isn't working.

To find out where the break happens start with the SceneDelegate having

let contentView = ContentView().environment(\.managedObjectContext, context)

Then see where the connection with Environment is being broken

Put something like this in all your views to see which doesn't get the Environment variable.

@Environment(\.managedObjectContext) var managedObjectContext
     var body: some View {
         print("MOC = ")
         print(managedObjectContext.name ?? "broken")
     return Text("Hello World!")
}

When you find the break reestablish it with

YourBrokenView().environment(\.managedObjectContext, managedObjectContext)
Sign up to request clarification or add additional context in comments.

4 Comments

That worked! Whats the problem with my @Environment var?
See above the connection is probably being broken
that print returns empty for broken
btw since its a mac app it doesn't have SceneDelegate
1

This is what worked for me I was using NSManagedObject subclases (Manual/None on the codegen definition ) for my CoreData entity cuz I was making some unwrapping. I have to delete the class and use the editor to generate again then all worked again

Comments

0

In the @enviromentVAr, sometimes, you need to set the enviroment by yourself.

 let managedObjectContext: NSManagedObjectContext =  ((UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext)!

 ContentView().environment(\.managedObjectContext, managedObjectContext)

Then the managedObjectContext can work.

 @Environment(\.managedObjectContext) var managedObjectContext

Comments

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.