0

[UIDocument saveToURL:forSaveOperation:completionHandler:]调用时的行为是什么UIDocumentSaveForCreating


方法#1 - saveToURL:保存后关闭文档

然后无需关闭文档。

MYDocument *document = [[MYDocument alloc] initWithFileURL:fileURL];
[document saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL saveSuccess) {
    if (!saveSuccess) {
        ALog(@"Failed to create file at %@", fileURL);
        failureBlock();
        return;
    }
    successBlock(fileURL);
}];

方法#2 - saveToURL:保存后不关闭文档

然后我必须自己关闭文档,如下所示:

MYDocument *document = [[MYDocument alloc] initWithFileURL:fileURL];
[document saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL saveSuccess) {
    if (!saveSuccess) {
        ALog(@"Failed to create file at %@", fileURL);
        failureBlock();
        return;
    }
    [document closeWithCompletionHandler:^(BOOL closeSuccess) {
        if(!closeSuccess) {
            ALog(@"Error during close after creating %@", fileURL);
            failureBlock();
            return;
        }
        successBlock(fileURL);
    }];
}];
4

1 回答 1

0

啊哈意识到 UIDocument 有一个documentState属性。

在第 1 步中,documentState 返回UIDocumentStateNormal,因此它是打开的。

在第 2 步中,documentState 返回UIDocumentStateClosed,因此它被关闭。

MYDocument *document = [[MYDocument alloc] initWithFileURL:fileURL];
[document saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL saveSuccess) {
    if (!saveSuccess) {
        NSLog(@"Failed to create file at %@", fileURL);
        failureBlock();
        return;
    }
    NSLog(@"step1: documentState: %d", document.documentState);
    [document closeWithCompletionHandler:^(BOOL closeSuccess) {
        if(!closeSuccess) {
            NSLog(@"Error during close after creating %@", fileURL);
            failureBlock();
            return;
        }
        NSLog(@"step2: documentState: %d", document.documentState);
        successBlock(fileURL);
    }];
}];

所以NO是我自己的问题的答案

UIDocumentSaveForCreating 是否在创建后关闭文档?

于 2013-10-08T19:46:29.900 回答