I've read the documentation and followed the steps in this post -
Still I'm getting errors when running my code as follows
-(void)loadPhotoGallery:(NSURL *)path
{
if (self.defaultPhotoLibrary != nil) return;
// this should handle auto migrations - according to documentation
NSURL * url;
if (path == nil)
{
url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"defaultPhotoDatabase"];
} else {
url = path;
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path1];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
NSError *error;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error]) {
// Printing this error
NSLog(@"Problem with PersistentStoreCoordinator: %@",error);
}
UIManagedDocument *lib = [[UIManagedDocument alloc] initWithFileURL:url];
if (![[NSFileManager defaultManager] fileExistsAtPath:[lib.fileURL path]]) {
[lib saveToURL:lib.fileURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success) {
if (success)
{
// Great
} else {
//bad
}
}
else if (lib.documentState == UIDocumentStateClosed) {
[lib openWithCompletionHandler:^(BOOL success) {
if (success) // here is the failure when migrating - because the DB file exists
{
//great!
} else {
//bad - getting into this code
}
}
error I'm getting:
Problem with PersistentStoreCoordinator: Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0xe09a880 {NSUnderlyingException=unable to open database file, NSSQLiteErrorDomain=14}
But the database file does exist (goes in the "elseif") - so not clear what the problem is?