2

I'm working on a static library using Core Data framework. Sadly, I'm unable to access my Bundle / .momd file generated.

What I've done :

  • Create my Data Model (Model.xcdatamodeld file)
  • Create a new Bundle target (named sdkResources) where I've added my xcdatamodeld file in Compile Sources and linked CoreData.framework
  • Inside my sdk target, I've put sdkResources.bundle inside "Copy Bundle Resources" section
  • Edit my sdk scheme and added sdkResources target in the build section

My not working code :

NSString *staticLibraryBundlePath = [[NSBundle mainBundle] pathForResource:@"sdkResources" ofType:@"bundle"];
NSURL *staticLibraryMOMURL = [[NSBundle bundleWithPath:staticLibraryBundlePath] URLForResource:@"Model" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:staticLibraryMOMURL];

Returns :

[FAILED], NSInvalidArgumentException "Cannot create an NSPersistentStoreCoordinator with a nil model" raised

The problem seems to be the "mainBundle" access because the code below is working when I run my sdk tests (at least no error)

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *staticLibraryMOMURL = [bundle URLForResource:@"Model" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:staticLibraryMOMURL];

But when I want to use my sdk inside an other App (using a podFile file) I can't access my bundle and get the same error :/

I've read and try a lot but nothing seems to work. I don't have a lot of experience in iOS dev and I'm trying my best to understand what's going on but it's a dead end to me now :(

Regards

  • Edit / Screens of my project configuration

Bundle build phase

sdk scheme configuration

Sdk build phase

I don't know why sdkResources.bundle is red.. Hope this is what you're asking for, thanks.

Edit : After fixing path to sdkResources.bundle enter image description here

Edit :

Ok, I'm really close to the solution, my bundle is successfully copied inside my final App but I can't access my Model and it's only working inside my sdk tests when I'm using [NSBundle bundleForClass:[self class]] instead of [NSBundle mainBundle]. It's probably just a stupid configuration but.. yeah.. I'll keep digging !

MajorShepard
  • 403
  • 4
  • 14

4 Answers4

3

Ok, I got it !

After (too) many hours searching to put my Model inside a Bundle I decided to just keep it inside my sdk and check my podspec configuration (since it was working in my sdk tests but not in my final project)

This is my solution, inside my podspec file I just added :

s.resources      = 'sdk/**/*.{xcdatamodeld,xcdatamodel}'
s.preserve_paths = 'sdk/**/*'
s.framework = 'CoreData'

Basically, I'm just adding my model as a resources and CoreData framework. At least I learned a lot about Bundle and podspec configuration...

MajorShepard
  • 403
  • 4
  • 14
0

If it is red, the file doesn't exist. If you are sure it exists, delete the red file and add it again: This is likely a misconfigured file link

Edit to add: often, you have to build the library first in order to generate the file. Is there a target for this library? If so, select it, build and then switch back to your project

davbryn
  • 7,156
  • 2
  • 24
  • 47
  • When I've created my Bundle target, Xcode generated a 'sdkResources.bundle' file inside my "Products" group but eveything in there seems to be missing even after a successful build of my bundle. And yes, I've a target for my sdk and my bundle. My sdk needs bundle to work. – MajorShepard Feb 15 '15 at 12:14
  • Does the file exist where you assume it does? If it does, link to it. The red link means file not found – davbryn Feb 15 '15 at 12:57
  • It does and when I click on the sdkResources.bundle in the product group, I can see the "Full path" property which is correct ! How do I link to it (since it's already correct..) ? I've no control on files in the "product group" (delete, add..) Sorry, I feel so retarded right now... – MajorShepard Feb 15 '15 at 14:20
  • Delete the red linked item and drag the good item into the same window. – davbryn Feb 15 '15 at 14:49
  • Ok, I'm really close to the solution (I think...) When I manually add my sdkResources.bundle in "Copy bundle Resources" Build Phases it "pops" inside my SDK files tree BUT [[NSBundle mainBundle] pathForResource:@"sdkResources" ofType:@"bundle"] still returns null :/ – MajorShepard Feb 15 '15 at 15:30
0

From the screenshot we see that while the bundle is meant to be copied, it doesn't exist.
=> THAT is fine it will be built later maybe

BUT

as we can also see the sdk target has no dependency to sdkResoures.
the bundle is NOT built in time

setting it to be build in the scheme doesn't do it (the scheme would be too late!)
target needs to depend on the bundle target

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Ok I understand. I've added sdkResources inside "Target dependencies" of my sdk target but it still doesn't work except in my sdkTests :/ Do I miss something else ? Do I need to set something inside my podspec file for example ? – MajorShepard Feb 15 '15 at 13:27
0

I think the solution is may be much simpler if you are not using Pods and using the framework only. You will have to change the Bundle you are looking for your momd in.

Please check this article here

The solution proposed is

    lazy var managedObjectModel: NSManagedObjectModel = {
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
    let carKitBundle = NSBundle(identifier: "com.andrewcbancroft.CarKit")

    let modelURL = carKitBundle!.URLForResource("CarModel", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

Here com.andrewcbancroft.CarKit the bundle identifier of your framework and that is the bundle that contains the momd file that you want to access from your code

Mohamed Saleh
  • 2,881
  • 1
  • 23
  • 35