在自定义静态框架iOS中使用magicalrecords库

我一直在为iOS实现自定义静态框架。 一切都运作良好,但现在我意识到我需要通过框架中的coredata存储信息。 我一直在使用magicalrecord库和我之前的项目,我想知道是否有人有任何经验将magicalrecord集成到您自己的自定义静态框架中。

当我在框架代码中调用setupcorestack方法时,没有任何反应。

这是我们如何做到的:

// 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; } 

注意:似乎可以有多个持久存储和多个数据库,但我们还没有尝试过这样做或者还没有需要。 但是,如果您确实需要多个持久存储,您可能还会参考其他SOpost:

使用MagicalRecord的多个数据库或仅将部分数据库同步到iCloud

我是这样做的:

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