Using magicalrecords library in iOS custom static structure

I am using a custom static environment for iOS. Everything works well, but now I realized that I need store information through coredata within. I used the magicalrecord library with my previous projects, and I was wondering if anyone had any experience integrating magicalrecord into your own static structure.

When I call the setupcorestack method inside the framework code, nothing happens.

+4
source share
2 answers

Here's how we did it:

// 1: Note that all setup is done within the AppDelegate of the project (not the framework) - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 2: Locate your framework bundle NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath]; NSString *frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"Your-Framework-Bundle-Name.bundle"]; NSBundle *frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath]; // 3: Create an NSManagedObject Model by merging all models from the project and your framework. This simplifies saving as you can use a single persistent store coordinator to save a single managed object model. NSArray *bundles = [NSArray arrayWithObjects:[NSBundle mainBundle], frameworkBundle, nil]; NSManagedObjectModel *models = [NSManagedObjectModel mergedModelFromBundles:bundles]; [MagicalRecord setShouldAutoCreateManagedObjectModel:NO]; [NSManagedObjectModel setDefaultManagedObjectModel:models]; [MagicalRecord setupCoreDataStackWithStoreNamed:@"Your-Store-Name.sqlite"]; // Project specific setup goes here... return YES; } 

Note. It seems possible to have several persistent repositories and several databases, but we have not tried to do this or have not yet needed. However, if you need several persistent stores in your case, you can also refer to this other SO post:

Multiple databases with MagicalRecord or sync only part of the database with iCloud

+6
source

I do it like this:

 NSManagedObjectModel *model = [NSManagedObjectModel MR_newModelNamed:@"MyModel.momd" inBundleNamed:@"myOtherResource.bundle"]; [NSManagedObjectModel MR_setDefaultManagedObjectModel:model]; //... continue to setup CoreDataStack after here 
-1
source

All Articles