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

时间:2013-04-02 18:37:00

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

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

当我在框架代码中调用setupcorestack方法时,没有任何事情发生。

2 个答案:

答案 0 :(得分: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帖子:

Multiple databases with MagicalRecord or sync only part of database to iCloud

答案 1 :(得分:-1)

我这样做:

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

//... continue to setup CoreDataStack after here