SwiftUI + document based application problems

I am trying to build a document based SwiftUI application with core data enabled. Starting from the template in Xcode (11.5): New project -> macOS + App -> Swift + SwiftUI + "Create document-based application" + "Use Core data". After that I try to add an entity in model editor with just two attributes, "id" and "amount". The entity is called "Trans" and codegen is set on "Class Definition".

In the provided Content view I add the code below so I can access the managed object context.

@Environment(\.managedObjectContext) var moc

So far it's working as expected. After that I try to add a simple fetch request.

@FetchRequest(entity: Trans.entity(), sortDescriptors: []) var trans: FetchedResults<Trans>

Then this error comes up in the console.

No NSEntityDescriptions in any model claim the NSManagedObject subclass 'myapp.Trans' so +entity is confused. Have you loaded your NSManagedObjectModel yet?

I am quite new to Core Data so I don't even know where to start looking. Is this broken? Or am I supposed to add more code to get it to work? Can someone please point in the right direction?
With following your description (which seems to be the right procedure for Core Data apps), I got another error

Use of undeclared type 'Trans'

Seems the build process of Xcode is sort of broken. Have you tried Clean-build (Product > Clean Build Folder)?
Thanks for the answer. Yepp, multiple times.. Recreated the project multiple times also.

Yepp, multiple times.. Recreated the project multiple times also.

Seems you are trapped in a deeper hole than mine.

In my case, Xcode insists showing errors in the editor pane, but build succeeds and can launch simulator.

Please try these.
  • Comment out all parts using Trans, also comment out all parts depending on it.

  • Clean-build and confirm it succeeds.

  • Uncomment a line (carefully choose one which does not cause error) and build, one by one.

This is the procedure when I find some errors possibly caused by Xcode's build process.
Not sure, but hope it works for your case.
I came up with this crash in the original question last week. I found a quick fix that works for me.

First, make an new file with an extension to the managed object (replace Note with your object name):

Code Block swift
extension Note {
    class func swiftUIFetchRequest() -> NSFetchRequest<Note> {
        let request = NSFetchRequest<Note>(entityName: "Note")
        request.sortDescriptors = [] /* You can add custom sort descriptors here */
        return request
    }
}


Then, start using FetchRequest like this (again, replacing Note with your object name):
Code Block swift
@FetchRequest(fetchRequest: Note.swiftUIFetchRequest()) var allNotes: FetchedResults<Note>


Let me know if it works for you.

I think the entity initializer is a little bit broken in beta 1.
Tom, thanks for taking time to share your solution. It did solve my problem, thanks alot! Now there are other problems that I need to look into.

When I try to save something to Core data following error appears:

[error] error: Illegal attempt to save to a file that was never opened. "This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.". No last error recorded.

My save code:

Code Block
let t = Trans(context: self.moc)
t.id = UUID()
t.amount = 128
do {
try self.moc.save()
} catch let error as NSError {
print(error)
}


Anyone that has an idea?

SwiftUI + document based application problems
 
 
Q