1

UIManagedDocument から派生したコンテキストを持つ CoreData に関する質問があります。

以下のこのスニペットでは、「ドキュメントを開く際のエラー」は記録されませんが、常に「ドキュメントはまだ閉じられています」と記録されます。ドキュメントを開けないのはなぜですか? アイデアはありますか?

-(void)openDocument
{
    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory     inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Default Date Database"];

    UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url];

    if ([[NSFileManager defaultManager] fileExistsAtPath:[url path]])
    {
        [document openWithCompletionHandler:^(BOOL success){
            if (!success) {
            // Handle the error.
            NSLog(@"Error opening the document");
           }
        }];
    }
    else
    {
        [document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
        if (!success) {
            // Handle the error.
            NSLog(@"Error saving the file");
            }
        }];
    }

    self.theDocument = document;


    if (self.theDocument.documentState == UIDocumentStateClosed)
    {
        NSLog(@"Document still closed!");
    }

}
4

1 に答える 1

2

openWithCompletionHandler非同期メソッドです。ドキュメントを開いて読み取るためのバックグラウンド スレッドのみを開始します。を確認するとdocumentState、このバックグラウンド スレッドはおそらくまだ終了していないため、状態はまだ "closed" です。

openWithCompletionHandlerドキュメントが開かれたとき (または失敗したとき) に completionHandler ブロックを実行します。

于 2012-08-22T22:32:17.523 に答える