UIDocumentSaveForCreating在创建后关闭文档吗?

时间:2013-06-08 14:57:24

标签: ios icloud uidocument

使用[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);
    }];
}];

1 个答案:

答案 0 :(得分:0)

Aha意识到UIDocument有documentState属性。

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

在第二步中,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在创建后是否关闭文档?