iosobjective-ccore-datamagicalrecordcocoa-touch

How to use MagicalRecord Library in cocoa touch framework?


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?


Solution

  • 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];