1
votes

we made a cocoa touch framework which saves and fetches data from a local database. We used core-data and MagicalRecord library for this.

When we integrate our framework with some app and try to add data to database, we are getting a crash indicating that the entity doesn't exist. So, we tried printing the list of entities and got an empty array.

The following question is similar but that is for static library - Using magicalrecords library in custom static framework iOS

We tried the accepted answer and it didn't solve the problem because the framework bundle path doesn't exist for cocoa touch framework

Does anyone has any solution in case of cocoa touch framework?

2
Can anyone please tell me why this question got downvoted? I didn't find answer for this question in stackoverflow. So, I posted it. If you know a similar question, please redirect me to the link.KrishnaCA
I'm facing the same problem. I wonder where to setup the Core Data Stackfruechtemuesli
Hi, we resolved the problem. You can setup the Core Data Stack in the framework itself. I will add the answer.KrishnaCA

2 Answers

3
votes

You can set the Core Data Stack inside the framework and initialize it with the following lines inside the framework.

 NSArray *bundles = @[[NSBundle bundleWithIdentifier:YOUR_FRAMEWORKS_BUNDLE_IDENTIFIER], [NSBundle mainBundle]];
[MagicalRecord setShouldAutoCreateManagedObjectModel:NO];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:bundles]];
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:YOUR_COREDATA_MODEL_NAME];
0
votes

Thanks krishnaCA! Works like a charm. Here's a swift version of the code:

func setupCoreDataStack(_ storeFile:String){

    guard let frameworkBundle = Bundle.init(identifier: "bundleID") else { return }
    MagicalRecord.setShouldAutoCreateManagedObjectModel(false)
    NSManagedObjectModel.mr_setDefaultManagedObjectModel(NSManagedObjectModel.mergedModel(from: [frameworkBundle,Bundle.main]))
    MagicalRecord.setupCoreDataStack(withAutoMigratingSqliteStoreNamed: storeFile)
}