iam*_*hen 4 frameworks core-data static-libraries ios magicalrecord
我一直在为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;
}
Run Code Online (Sandbox Code Playgroud)
注意:似乎可能有多个持久存储和多个数据库,但我们还没有尝试过这样做或者还没有需要.但是,如果您确实需要多个持久存储,您可能还会参考其他SO帖子:
使用MagicalRecord的多个数据库或仅将部分数据库同步到iCloud
| 归档时间: |
|
| 查看次数: |
1501 次 |
| 最近记录: |