ドキュメントを読み、この投稿の手順に従いました-
次のようにコードを実行すると、まだエラーが発生します
-(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
}
}
私が得ているエラー:
PersistentStoreCoordinator の問題: エラー Domain=NSCocoaErrorDomain Code=256 「操作を完了できませんでした。(Cocoa エラー 256.)」 UserInfo=0xe09a880 {NSUnderlyingException=データベース ファイルを開けません、NSSQLiteErrorDomain=14}
しかし、データベース ファイルは存在します ("elseif" に入ります)。