0

I'm making an app which uses Core Data, and for the purpose of my app I need to bundle a pre-populated database with the app bundle.

I'm using a UIManagedDocument

For this, I created the database with the app and later loaded the data. Then I searched the iOS Simulator folder in my Mac and drag and dropped the persistentStore file into the app bundle.

Later in the code I copied it to the NSDocumentDirectory because the user will be allowed to edit the database at runtime. This was done with the following method:

if(![[NSFileManager defaultManager] fileExistsAtPath:[self.appDatabase.fileURL path]])
{        
    // COPY FROM BUNDLE

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *DB = [[paths lastObject] stringByAppendingPathComponent:@"Default App Database"];
    DB = [DB stringByAppendingPathComponent:@"StoreContent"];

    [fileManager createDirectoryAtPath:DB withIntermediateDirectories:YES attributes:nil error:&error];

    NSLog(@"create directory error: %@",error);

    DB = [DB stringByAppendingPathComponent:@"persistentStore"];

    NSString *shippedDB = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"persistentStore"];
    NSLog(@"%d",[fileManager fileExistsAtPath:shippedDB]);
    [fileManager copyItemAtPath:shippedDB toPath:DB error:&error];

    NSLog(@"Copy error %@",error);
}

Everything is working perfectly! But, I'm not sure if this will generate some type of error. So my question, is it OK to handle a Core Data database created with from an UIManagedDocument with NSFileManager?

And, will Apple complain in the review process for bundling the persistentStore file?

tomidelucca
  • 2,543
  • 1
  • 26
  • 26

1 Answers1

1

Bundling a default persistent store with the app is explicitly mentioned in the "Core Data Programming Guide":

How do I initialize a store with default data?

...

You can create a separate persistent store that contains the default data and include the store as an application resource. When you want to use it, you must either copy the whole store to a suitable location, or copy the objects from the defaults store to an existing store.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382