我一直在为iOS实现一个自定义静态框架。一切都运作良好,但现在我意识到我需要通过coredata在框架中存储信息。我之前的项目一直使用magicalrecord库,我想知道是否有人有将magicalrecord集成到自己的自定义静态框架中的经验。
当我在框架代码中调用setupcorestack方法时,什么都没有发生。
我一直在为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;
}
[MagicalRecord setShouldAutoCreateManagedObjectModel:NO];
谢谢!! - Kevin DelordNSManagedObjectModel *model = [NSManagedObjectModel MR_newModelNamed:@"MyModel.momd" inBundleNamed:@"myOtherResource.bundle"];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:model];
//... continue to setup CoreDataStack after here