4

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

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

4

2 回答 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

于 2013-04-05T17:54:23.067 回答
-1

我这样做:

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

//... continue to setup CoreDataStack after here
于 2013-10-17T19:50:24.440 回答