Question

The core data models are in a bundle which is added to main project. now i have to migrate core data to newer model. i have been following some example like this so i want to migrate database before any queries or import happen. so i placed the code in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions but the bundle which contains core data models is not loaded at that time. getting hold of bundle says its not yet loaded. So I can't start the migration process.

how can i force it to load this bundle?

is it a good way to have the core data models in a separate bundle other then the main bundle?

edit:

- (NSManagedObjectModel*)objectModel
{
if (_objectModel)
    return _objectModel;

    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:kDataManagerBundleName ofType:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

NSString *modelPath = [bundle pathForResource:kDataManagerModelName ofType:@"momd"];
_objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];

return _objectModel;
}

for the migration part it says bundle not yet loaded and returns nil for pathForResource.

Was it helpful?

Solution 2

I ended up with moving the data model in main bundle as there is no way to load resources bundle on launching.

OTHER TIPS

You can use the NSBundle method load to force to load a bundle (docs here). EDIT: As you said, this is only for executable code and not for resources.

About having CoreData models in a separate bundle, it's a common practice if you want to reuse that model(and/or any other resource) in several apps. If you don't want that there is no reason to have a separate bundle IMO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top