【问题标题】:Prevent crash when trying to close UIDocument尝试关闭 UIDocument 时防止崩溃
【发布时间】:2012-07-06 14:32:10
【问题描述】:

我想知道在尝试关闭 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;
        }

    }];             
}

【问题讨论】:

    标签: objective-c ios icloud uidocument


    【解决方案1】:

    看起来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;
            }
    
        }];             
    }
    

    【讨论】:

    • 非常感谢。我完全忽略了关闭与关闭不同。 iCloud API 离完美还很遥远……
    【解决方案2】:

    知道每个 UIDocument 对象只能打开和关闭一次是非常重要的。在意识到这一点之前,我在使用 UIDocuments 时遇到了很多奇怪的问题,无论是在云中还是在本地文件中。当你关闭一个文档时,将它的指针设置为 nil,这样你就不能再次关闭它了。如果您以后需要再次访问同一个文件,请使用相同的 fileURL 创建一个新的 UIDocument。

    您在上面显示的错误消息是您尝试重复使用文档时出现的错误消息。

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 2012-08-09
      • 1970-01-01
      • 2017-04-27
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      相关资源
      最近更新 更多