0
votes

I am trying to use a preloaded Sqlite database for my universal app, which uses core data. It use to work with iOS 4.3, but not with iOS 5.0+ and I'm using Xcode 4.3.

I've looked at the linked below, made changes, changed back, but to no avail and now the preloading does not work at all.

Prepopulate Core Data in iOS 5

coredata problem nsurl may not respond to stringByAppendingPathComponent

What am I doing wrong, I have listed my code below, any help would be greatly appreciated. Thank you for your time.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{

    if (persistentStoreCoordinator != nil) 
    {
        return persistentStoreCoordinator;
    }

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"SAP_ECC_DB.sqlite"];
    //NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SAP_ECC_DB.sqlite"];

     //Set up the store.
     //For the sake of illustration, provide a pre-populated default store.
    NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:storePath]) 
    {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"SAP_ECC_DB" ofType:@"sqlite"];
        if (defaultStorePath) 
        {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
        }
    }
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];    
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

    NSError *error;
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    } 



- (NSString *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
    //return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
1
In what way doesn't it work? Does the file copy? Does the data not appear? Do you get errors?jrturton
The file does not copy and therefore no data appears. I get no errors.JingJingTao
can you confirm that it actually exists in the bundle? It should appear in the copy bundle resources portion of your build phases tab. Sometimes xcode 4.3 gets nasty and removes them for no reason.borrrden
Perhaps if you hadn't put error:NULL]; you might be able to see the error you're getting ;)deanWombourne
@borrrden, wow that worked, saved the day, I added it to the project, but I guess I needed to check the build phases tab to be sure. Thanks a lot, if you post an answer, I'll accept it.JingJingTao

1 Answers

2
votes

Sometimes, especially in XCode 4.3, even if you add a resource to the project file list it doesn't get included in the final product, or gets removed along the way for no reason. If you experience a problem where your product is not updating, not appearing, or not behaving as it should for whatever reason, check to make sure that it is actually included in the "Copy Bundle Resources" (sometimes even .m files go missing, those will be in the Compile Sources though) of the Build Phases tab of your project.

This one is especially tricky because Core Data will create a new database if one doesn't exist, making it appear as though it didn't copy, when in fact it didn't exist in the first place.