我在我的应用程序中使用 iCloud 来加载文本文件。加载文本文件时,当我调用_UIDocument openWithCompletionHandler:^(BOOL success)
等时,iCloud会调用此方法:
-(BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError {
NSLog(@"Library loadFromContents: state = %d", self.documentState);
if (!_books) {
_books = [[NSMutableArray alloc] init];
}
//if (self.documentState > 7) {
// NSLog(@"document is either savingError (4), EditingDisabled (8) or both (12)... will not load");
// return NO;
//}
self.books = [NSKeyedUnarchiver unarchiveObjectWithData:contents];
if ([_delegate respondsToSelector:@selector(libraryDocumentUpdated:)]) {
[_delegate libraryDocumentUpdated:self];
}
return YES;
}
现在最大的问题是 documentState 是 8 (UIDocumentStateEditingDisabled) 还是 12 (UIDocumentStateSavingError & UIDocumentStateEditingDisabled)。这通常会导致应用程序崩溃。如果 documentState > 7,即如果它是 8 或 12,我尝试返回 NO,但这会导致根本不加载任何内容。
我想问题是 UIDocument 不会加载任何东西到 self.books 如果它的编辑被禁用或者如果有一个保存错误。
处理此类错误的好做法是什么?另外,为什么 Apple 没有在他们的示例代码中建议在将数据加载到 UIDocument ( iCloud Docs ) 之前检查 documentState ?我想我在做一些根本错误的事情。