2012-07-06 112 views
1

我想知道如何在嘗試關閉UIDoc兩次時防止崩潰。我試圖確定在我的代碼中,你(理論上)不能關閉UIDocument兩次。但是,它有時會發生,我不知道爲什麼。如果是這樣,應用程序崩潰:嘗試關閉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; 
     } 

    }];    
} 

回答

3

它看起來像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; 
     } 

    }];    
} 
+0

非常感謝。我完全忽略了這個收盤與收盤不一樣。 iCloud API仍然遠非完美... – 2012-07-06 14:46:16

0

要知道,每個UIDocument對象只能打開和關閉一次,這是非常重要的。在實現這個之前,我在UIDocuments中遇到了很多奇怪的問題,無論是在雲端還是在本地文件中。關閉文檔時,將其指針設置爲零,因此無法再將其關閉。如果您稍後需要再次訪問相同的文件,請使用相同的fileURL創建一個新的UIDocument。

上面顯示的錯誤消息是您嘗試重新使用文檔時出現的錯誤消息。