Using magicalrecords library in custom static fram

2019-03-30 21:10发布

I've been implementing a custom static framework for iOS. Everything is working well, but now I realized that I need a store information via coredata in the framework. I've been using the magicalrecord library with my previous projects and I was wondering if anyone has any experience integrating magicalrecord into your own custom static framework.

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

2条回答
Root(大扎)
2楼-- · 2019-03-30 21:16

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
查看更多
来,给爷笑一个
3楼-- · 2019-03-30 21:40

Here's how we've done 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 to be possible to have multiple persistent stores and multiple databases, but we haven't tried doing this or have had a need to as of yet. However, if you do need multiple persistent stores in your case, you might also refer to this other SO post:

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

查看更多
登录 后发表回答