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

4

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

当我在框架代码中调用setupcorestack方法时,什么都没有发生。


你正在为你的自定义库/框架提供所需的头文件。然后,使用你的库/框架的应用程序需要链接到所有所需的库文件/目标文件/框架。 - Till
2个回答

6

这是我们的做法:

// 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 帖子: “使用 MagicalRecord 进行多个数据库或仅同步部分数据库到 iCloud”。

嗨,上述解决方案在cocoa-touch-framework的情况下不起作用。 - KrishnaCA
这个回答已经将近三年了...随着当前版本的更新,事情可能已经发生了变化...如果你找到了一个更新/更好的解决方案,请随时发布一个新的答案。 - JRG-Developer
谢谢回复。 - KrishnaCA
4年过去了,但仍然准确!我所缺少的是: [MagicalRecord setShouldAutoCreateManagedObjectModel:NO]; 谢谢!! - Kevin Delord

-1
我是这样做的:
NSManagedObjectModel *model = [NSManagedObjectModel MR_newModelNamed:@"MyModel.momd" inBundleNamed:@"myOtherResource.bundle"];
[NSManagedObjectModel  MR_setDefaultManagedObjectModel:model];

//... continue to setup CoreDataStack after here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接