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

标签 ios core-data frameworks static-libraries magicalrecord

我一直在为 iOS 实现自定义静态框架。一切正常,但现在我意识到我需要通过框架中的核心数据获取商店信息。我在以前的项目中一直使用 magicalrecord 库,我想知道是否有人有将 magicalrecord 集成到您自己的自定义静态框架中的经验。

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

最佳答案

这是我们的做法:

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

关于ios - 在自定义静态框架 iOS 中使用 magicalrecords 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15771751/

相关文章:

javascript - 在 iOS Safari navigator.plugins 中找不到 Acrobat Reader

Xcode-错误 : pathspec '...' did not match any file(s) known to git

ios - 在应用程序关闭之前,Core Data 不会保存数据

ios - 将 NSManagedObject 转换为子类返回 nil

java - 类似 Django 的 Java 框架

c# - 是否有任何适用于 Windows 窗体、数据库驱动的应用程序开发/原型(prototype)设计的框架?

基于 Java 的 Web 框架替代品

objective-c - 如何在 UIMarkupTextPrintFormatter 中添加图片?

c# - iOS 和 .NET 的 Base64 编码区别

ios - 如何快速循环遍历一组对象