I am using UIManagedDocument’s Context to interact with my Core Data Model. However, I would like to initially load the Persistant Store contact from a local sqlite database that I have. I believe I am supposed to use configurePersistentStoreCoordinatorForURL: but have no idea how to do it. All I found on the Apple store was an example with App Delegate Core Data not this function or UIManagedDocument way.
My source sqlite database in resource folder is called source_from.sqlite and the Data model is called Source.xcdatamodeld
Also I prefer to have the new sqlite db in the document package. Not sure if the migration will happen automatically?
Thanks for your help,
Ross
Here is what I have and planning to do it in the initialize method:
-(void) initializeSourceDatabaseWithData:(UIManagedDocument *) sourceDatabase{
// if no persistent store for the uidocument preload it
// from sqlite file
}
-(void) useDocument{
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.sourceDatabase.fileURL path]]){
[self.sourceDatabase saveToURL:self.sourceDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
[self setupFetchedResultController];
[self initializeSourceDatabaseWithData:self.sourceDatabase];
}];
}else if (self.sourceDatabase.documentState == UIDocumentStateClosed){
[self.sourceDatabase openWithCompletionHandler:^(BOOL success){
[self setupFetchedResultController];
}];
}else if(self.sourceDatabase.documentState == UIDocumentStateNormal){
[self setupFetchedResultController];
}
}
-(void) setSourceDatabase:(UIManagedDocument *)sourceDatabase{
if (sourceDatabase != _sourceDatabase){
sourceDatabase = _sourceDatabase;
[self useDocument];
}
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (!self.sourceDatabase){
NSURL * url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Source Database"];
self.sourceDatabase = [[UIManagedDocument alloc] initWithFileURL:url];
}
}
I believe the best way is to subclass UIManagedDocument Class and implement the PersistanceStoreConfiguration so that it loads the file from outside the document package.