0

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.

Community
  • 1
  • 1
Hashmat Khalil
  • 1,826
  • 1
  • 25
  • 49

2 Answers2

0

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.

e1985
  • 6,239
  • 1
  • 24
  • 39
  • thanks for answering. load is for loading executable code. i'm looking for loading core data model(momd file). – Hashmat Khalil Sep 09 '13 at 13:13
  • You are right - just edited my answer. Could you paste a snippet of the code where you are creating the bundle and model? – e1985 Sep 09 '13 at 14:27
  • the model and bundle is created with xcode. the model is added to the bundle and the bundle is added to the target. there is no code behind it from my side. – Hashmat Khalil Sep 09 '13 at 14:37
  • But I imagine you are calling bundleWithURL: or bundleForPath: (to create the bundle object) and then also creating your managed object model somehow... Could you copy paste that code? – e1985 Sep 09 '13 at 15:07
  • i just posted that part. that part actually works. i mean i can read and save data, some of those reading happen right in the app did launch with options. – Hashmat Khalil Sep 09 '13 at 17:04
0

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

Hashmat Khalil
  • 1,826
  • 1
  • 25
  • 49