本文介绍了在自定义静态框架iOS中使用magicalrecords库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been implementing a custom static framework for iOS. Everything is working well, but now I realized that I need a store information via coredata in the framework. I've been using the magicalrecord library with my previous projects and I was wondering if anyone has any experience integrating magicalrecord into your own custom static framework.

当我调用

推荐答案

以下是我们的操作方式:

Here's how we've done it:

// 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信息:

Note: it seems to be possible to have multiple persistent stores and multiple databases, but we haven't tried doing this or have had a need to as of yet. However, if you do need multiple persistent stores in your case, you might also refer to this other SO post:

这篇关于在自定义静态框架iOS中使用magicalrecords库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 11:10