core data in a static library for the iPhone

后端 未结 8 1209
不知归路
不知归路 2020-11-28 02:07

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 .xcdatamod

8条回答
  •  [愿得一人]
    2020-11-28 02:33

    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.

提交回复
热议问题