Last active
December 24, 2015 06:19
-
-
Save tdhop/6756397 to your computer and use it in GitHub Desktop.
Open and initialize core data database that has been delivered with the app bundle. Note that text surrounded by <> will need to be replaced with local names for your app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copy <Database> to runtime location in Documents directory and open data store. | |
- (void) init<Database> | |
{ | |
/* Get URL of Documents directory. It returns all matching | |
directories in the domain but should only see one and it | |
is in location 0 of the array (or "lastObject") | |
*/ | |
NSArray *tempURLs = [[NSFileManager defaultManager] URLsForDirectory: NSDocumentDirectory inDomains:NSUserDomainMask]; | |
NSURL *documentsURL = [tempURLs lastObject]; | |
// Change working directory to Documents and create "<Database>/StoreContent" directory in run-time environment. | |
BOOL success = [[NSFileManager defaultManager] changeCurrentDirectoryPath:[documentsURL path]]; | |
NSLog(@"Change to Documents directory was successful? %d", success); | |
success = [[NSFileManager defaultManager] createDirectoryAtPath:@"./<Database>/StoreContent" withIntermediateDirectories:YES attributes:nil error:nil]; | |
NSLog(@"Create new directory was successful? %d", success); | |
/* Copy persistentStore into new directory from bundle. First build URL for file to be copied from bundle then build URL for file destination in Documents then copy file. | |
*/ | |
NSURL *copyFromURL = [[NSBundle mainBundle] resourceURL]; | |
copyFromURL = [copyFromURL URLByAppendingPathComponent:@"<DatabaseBundleFilename>"]; | |
NSURL *<DatabaseDestinationURL> = [documentsURL URLByAppendingPathComponent:@"<DatabaseSandboxFilename"]; | |
NSURL *copyToURL = [<DatabaseDestinationURL> URLByAppendingPathComponent:@"StoreContent/persistentStore"]; | |
success = [[NSFileManager defaultManager] copyItemAtURL:copyFromURL toURL:copyToURL error:nil]; | |
NSLog(@"File copy was successful? %d", success); | |
// Allocate UIManagedDoc object pointer to <Database> and open file if exists. | |
self.<DatabaseUIManagedDoc> = [[UIManagedDocument alloc] initWithFileURL:<DatabaseDestinationURL>]; | |
if ([[NSFileManager defaultManager] fileExistsAtPath:[<DatabaseDestinationURL> path]]) | |
{ | |
[self.<DatabaseUIManagedDoc> openWithCompletionHandler:^(BOOL success) | |
{ | |
// File should have been copied from main bundle. If not there - PROBLEM! | |
if (!success) | |
{ | |
NSLog(@"Couldn't open file at %@", <DatabaseDestinationURL>); | |
} | |
else | |
{ | |
NSLog(@"file open success=%d", success); | |
} | |
[self <AppDefinedAction>]; | |
}]; | |
} else NSLog(@"<Database> file not found"); | |
// Set <Database> context and save in App Delegate for | |
// use by View Controllers. | |
self.<DatabaseContext> = self.<DatabaseUIManagedDoc>.managedObjectContext; | |
self.myAppDelegate = [[UIApplication sharedApplication] delegate]; | |
[self.myAppDelegate setProdRegistryContext: self.<DatabaseContext>]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment