Stephen Donnell
Stephen Donnell

Reputation: 810

Swift - Sharing Core Data Model in App Group (w/ extension)

I'm trying to use the same core data model in my app as well as an extension, but am unsure how to share the core data model between the 2. The class for the core data model uses a namespace with the classname, so when I try and fetch the objects in the extension I get the unable to load class named "" error.

CoreData: warning: Unable to load class named 'Dali.Alarm' for entity 'Alarm'. Class not found, using default NSManagedObject instead.

Is there anyway to not use a namespace in the classname, or is there a way to make the extension inherit the namespace of the main project?

Core Data Entity

Upvotes: 6

Views: 1881

Answers (1)

Steven Hovater
Steven Hovater

Reputation: 1419

I was stuck on this earlier. Seems like a real issue, and it wouldn't hurt to file it as a feature request/bug with Apple.

In the meantime, you can get around it with two steps. First, tag your NSManagedObject subclasses with @objc(ClassName). Simply insert it above the class declaration:

@objc(ClassName)
class ClassName: NSManagedObjectSubclass {
    @NSManaged var name : String
}

Second, go back to the managed object model, and remove the namespace from the "class" name field in the inspector for the Entity you're working with.

This worked for me today, after reading this: I can't use my core data model in two targets in a Swift project

Upvotes: 10

Related Questions