【问题标题】:Does UIDocumentSaveForCreating close the document after creation?UIDocumentSaveForCreating 是否在创建后关闭文档?
【发布时间】:2013-06-08 14:57:23
【问题描述】:

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


方法#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);
    }];
}];

【问题讨论】:

    标签: ios icloud uidocument


    【解决方案1】:

    啊哈意识到 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 会在创建后关闭文档吗?

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 2012-03-06
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 2014-03-04
      • 2020-10-06
      • 1970-01-01
      • 2010-09-05
      相关资源
      最近更新 更多