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

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

问题描述

我建立了一个大量使用Core Data框架的静态库。我可以成功地使用库在我的外部项目,但只有如果我包括.xcdatamodel文件在主项目。这是不太理想的,因为库的点是尽可能隐藏实现细节。

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?

推荐答案

我也创建了我自己的使用Core Data的静态库。除了静态库,我在项目中有另一个bundle目标,我有一个复制Bundle Resources项目,将一些图像和类似的东西复制到捆绑和一个Compile Sources构建阶段,其中我编译xcdatamodel。

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.

最后的包将包含所有必要的文件。在你的主项目依赖于静态库,你必须包括那个bundle。您的主要项目现在可以访问使用核心数据所需的妈妈文件。

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;
}


必须给出xcdatamodel,只有编译的mom文件需要包含。

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天全站免登陆