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?