can a dataModel of Coredata be part of any other bundle than main bundle

末鹿安然 提交于 2019-12-08 05:40:27
Leonardo

Yes you can, I don't know where the included bundle come from. If it is included from an xcode project within another project you have to make sure that your bundle is included in the main bundle. Have a look at this: How to include a bundle in main project xcode 4

For a quick check you can take a look at your application bundle with "Show package content" and see if the included bundle is there. Then, starting from there you have to look for the bundle containing your data model. This is what I did in a project of mine. The hardest part was to include the external bundle. I think you are almost there with your code.

NSBundle *bundle = [NSBundle mainBundle];    
NSString *includedModelPath = [bundle pathForResource:@"YourIncludedBundle" ofType:@"bundle"];
NSURL *includedModelURL = [[NSBundle bundleWithPath:includedModelPath] URLForResource:@"DataModel" withExtension:@"momd"];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSURL *storeUrl = [NSURL fileURLWithPath: [documentsDirectory stringByAppendingPathComponent: dbname]];
self.managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:includedModelURL];

I have just copied and paste from my code, and make it more verbose to explain better.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!