我一直在为 iOS 实现自定义静态框架。一切正常,但现在我意识到我需要通过框架中的核心数据获取商店信息。我在以前的项目中一直使用 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;
}
注意:似乎有多个持久存储和多个数据库是可能的,但我们还没有尝试这样做,或者到目前为止还没有这样做的需要。但是,如果您确实需要在您的案例中使用多个持久存储,您还可以引用其他 SO 帖子:
Multiple databases with MagicalRecord or sync only part of database to iCloud
关于ios - 在自定义静态框架 iOS 中使用 magicalrecords 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15771751/