iPhone 静态库中的核心数据 [英] core data in a static library for the iPhone

查看:17
本文介绍了iPhone 静态库中的核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个大量使用 Core Data 框架的静态库.我可以在我的外部项目中成功使用该库,但前提是我在主项目中包含 .xcdatamodel 文件.这不太理想,因为该库的目的是最大限度地隐藏实现细节.

在单独的问题中,我被告知我不能将资源与图书馆捆绑在一起(现在对我来说完全有意义).

那么有没有办法以编程方式允许发现"模型而不必将模型包含在主项目中?

解决方案

我还创建了自己的使用 Core Data 的静态库.除了静态库之外,我在项目中还有一个包目标,其中我有一个 Copy Bundle Resources 项,它将一些图像和类似的东西复制到包和一个编译源构建阶段,在那里我编译 xcdatamodel.

最终的包将包含所有必要的文件.在依赖于静态库的主项目中,您还必须包含该包.您的主项目现在可以访问使用核心数据所需的 mom 文件.

要将核心数据与包中的妈妈一起使用,您必须在代码中创建一个合并的托管对象模型(可能是主项目也有一些核心数据模型):

<前><代码>- (NSManagedObjectModel *) 合并的ManagedObjectModel{如果 (!mergedManagedObjectModel){NSMutableSet *allBundles = [[[NSMutableSet alloc] init] autorelease];[allBundles addObjectsFromArray: [NSBundle allBundles]];[allBundles addObjectsFromArray: [NSBundle allFrameworks]];合并的ManagedObjectModel = [[NSManagedObjectModel mergeModelFromBundles: [allBundles allObjects]] 保留];}返回合并的ManagedObjectModel;}

通过只包含包,您将不必提供 xcdatamodel,只需要包含已编译的 mom 文件.

I've built a static library that makes heavy use of the Core Data framework. I can successfully use the library in my external project, but ONLY if I include the .xcdatamodel file in the main project. That is less than ideal, since the point of the library was to hide implementation details to the maximum possible.

In a separate question, I was informed that I cannot bundle resources with a library (which makes complete sense to me now).

So is there a way to programatically allow the model to be 'discovered' without having to include the model in the main project?

解决方案

I also created my own static library that uses Core Data. Besides the static library I have a another bundle target in the project where I have a Copy Bundle Resources item, that copies some images and things like that into the bundle and a Compile Sources build phase, where I am compiling the xcdatamodel.

The final bundle will contain all the necessary files. In your main project that relies on the static library you have to include that bundle as well. Your main project will now have access to the mom file that is needed to use core data.

To use core data with the mom from the bundle you have to create a merged managed object model in your code (it might be the main project has some core data model as well):


- (NSManagedObjectModel *) mergedManagedObjectModel 
{   
    if (!mergedManagedObjectModel) 
    {
        NSMutableSet *allBundles = [[[NSMutableSet alloc] init] autorelease];
        [allBundles addObjectsFromArray: [NSBundle allBundles]];
        [allBundles addObjectsFromArray: [NSBundle allFrameworks]];

        mergedManagedObjectModel = [[NSManagedObjectModel mergedModelFromBundles: [allBundles allObjects]] retain];
    }

    return mergedManagedObjectModel;
}


By just including the bundle you will not have to give out the xcdatamodel, only the compiled mom file needs to be included.

这篇关于iPhone 静态库中的核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆