3

I am trying to create CoreDataKit using a static library.

It contains:

  1. xcdatamodel or model file
  2. NSManagedObject or Entity (

Since static library can have only objects files hence model file can't be part of static library.

To make it happen I created separate bundle having only model file and copied this bundle to my xcode project using this link ( core data in a static library for the iPhone)

Now, while loading Person entity

let personEntity = NSManagedObject(entity: entity, insertInto: context) as! Person 

I got below error:

Unable to load class named 'MyStaticLibraryModels.Person' for entity 'Person'.  Class not found, using default NSManagedObject instead.

and exception:

Could not cast value of type 'NSManagedObject_Person_' (0x600000243b40) to 'ONCoreDataKit.Person' (0x105d71a68).

The other approach could be to use framework which can contain model file and can be dynamically loaded.

Since loading dynamic framework can take some extra time I am interested in creating static library for faster access.

manismku
  • 2,160
  • 14
  • 24
  • At a rough guess, you might have packed the model up in the bundle, but this doesn't automatically result in it getting copied into the build folder – marko Feb 02 '19 at 17:29
  • I guess I am able to get model in build folder because following is working: lazy var managedObjectModel: NSManagedObjectModel = { let url = Bundle.main.path(forResource: "MyStaticLibraryModels", ofType: "bundle") let staticLibBundle = Bundle(path: url!) let modelURL = staticLibBundle!.url(forResource: "Model", withExtension: "mom")! return NSManagedObjectModel(contentsOf: modelURL)! }() – manismku Feb 03 '19 at 02:00
  • I am guessing since since model is now part of the main bundle and I am excessing Person from my "CoreDataKit" it's not able to get the Person entity. – manismku Feb 03 '19 at 02:03

0 Answers0