【问题标题】:Using magicalrecords library in custom static framework iOS在自定义静态框架iOS中使用magicrecords库
【发布时间】:2013-03-24 04:45:38
【问题描述】:

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

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

【问题讨论】:

  • 您正在为您的自定义库/框架提供所需的标头。然后,使用您的库/框架的应用程序需要链接到所有需要的库/对象文件/框架。

标签: ios core-data frameworks static-libraries magicalrecord


【解决方案1】:

我们是这样做的:

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

【讨论】:

  • 您好,上述解决方案不适用于 cocoa-touch-framework。
  • 这个答案已经有将近 3 年的历史了......当前版本的东西可能已经改变......如果您找到更新/更好的解决方案,请随时将其作为新答案发布。
  • 感谢回复
  • 4 岁但仍然准确!我缺少的是:[MagicalRecord setShouldAutoCreateManagedObjectModel:NO]; 谢谢!!
【解决方案2】:

我是这样做的:

NSManagedObjectModel *model = [NSManagedObjectModel MR_newModelNamed:@"MyModel.momd" inBundleNamed:@"myOtherResource.bundle"];
[NSManagedObjectModel  MR_setDefaultManagedObjectModel:model];

//... continue to setup CoreDataStack after here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多