objective-c - 尝试关闭UIDocument时防止崩溃

标签 objective-c ios icloud uidocument

我想知道如何在尝试两次关闭UIDoc时防止崩溃。我试图确保在我的代码中(理论上)您不能两次关闭UIDocument。但是,有时还是会发生,我不知道为什么。如果存在,则该应用程序崩溃:

2012-07-06 15:24:34.470 Meernotes[11620:707] ... doc state:Normal
2012-07-06 15:24:34.472 Meernotes[11620:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'closeWithCompletionHandler called while document is already closing'
*** First throw call stack:
(0x3720e88f 0x34f13259 0x3720e789 0x3720e7ab 0x312681d1 0xd19db 0x96f7f 0x9593f 0xacb8f 0x30f0cd23 0x37a7f933 0x371e2a33 0x371e2699 0x371e126f 0x371644a5 0x3716436d 0x33923439 0x30f10cd5 0x94fdd 0x94f78)
terminate called throwing an exception(lldb) 

我试图按以下方法防止崩溃,但是它没有任何作用(即仍然会崩溃):
-(void)closeDoc {

    UIDocumentState state = _selectedDocument.documentState;

    NSMutableArray * states = [NSMutableArray array];
    if (state == 0) {
        [states addObject:@"Normal"];
    }
    if (state & UIDocumentStateClosed) {
        [states addObject:@"Closed"];
    }
    if (state & UIDocumentStateInConflict) {
        [states addObject:@"In conflict"];
    }
    if (state & UIDocumentStateSavingError) {
        [states addObject:@"Saving error"];
    }
    if (state & UIDocumentStateEditingDisabled) {
        [states addObject:@"Editing disabled"];
    }
    NSLog(@"... doc state: %@", [states componentsJoinedByString:@", "]);

    if (_selectedDocument.documentState & UIDocumentStateClosed) return;

    [_selectedDocument closeWithCompletionHandler:^(BOOL success) {

        NSLog(@"Closed document.");
        // Check status
        if (!success) {
            NSLog(@"Failed to close %@", _selectedDocument.fileURL);
        } else {
            _selectedDocument = nil;
        }

    }];             
}

最佳答案

看来UIDocument不存储关闭状态,仅存储正常状态和关闭状态,因此您必须自己进行操作。

将此添加到您的类变量:

BOOL _documentClosing;

并在closeDoc方法中添加其用法:
-(void)closeDoc {

    if (_docClosing || (_selectedDocument.documentState & UIDocumentClosed) != 0)
        return;
    _docClosing = YES;

    [_selectedDocument closeWithCompletionHandler:^(BOOL success) {

        NSLog(@"Closed document.");
        // Check status
        if (!success) {
            NSLog(@"Failed to close %@", _selectedDocument.fileURL);
        } else {
            _selectedDocument = nil;
            _docClosing = NO;
        }

    }];             
}

关于objective-c - 尝试关闭UIDocument时防止崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11364236/

相关文章:

ios - 如何上传这张照片?

ios - 在 appdelegate 中以编程方式选择 tableview 单元格不起作用

iphone - 如何使用iOS SDK获取所有DNS记录

在 uialertview 中添加文本字段的 iphone 验证

ios - 点击单元格以下载文件并向上或向下滚动表格时,应用崩溃

ios - 更改 iCloud 帐户时 NSUbiquitousKeyValueStore 同步延迟

Objective-C:如果(对象)与。如果(对象!=无)

iphone - 创建自定义 UIGestureRecognizer

ios - HomeKit 抛出错误 77 和 78

ios - 通过 CloudKit 将 iCloud 用于用户偏好设置