【问题标题】:iCloud's loadFromContents - how to deal with UIDocumentStateSavingError & UIDocumentStateEditingDisablediCloud的loadFromContents——如何处理UIDocumentStateSavingError & UIDocumentStateEditingDisabled
【发布时间】:2011-12-24 19:33:25
【问题描述】:

我在我的应用程序中使用 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 的编辑被禁用或存在保存错误,则 UIDocument 不会将任何内容加载到 self.books 中。

处理此类错误的最佳做法是什么?另外,为什么 Apple 没有在他们的示例代码中建议在将数据加载到 UIDocument (iCloud Docs) 之前检查 documentState?我想我在做一些根本错误的事情。

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios5 icloud


    【解决方案1】:

    您是否实施了冲突管理?

    在这些情况下你应该尝试一些事情然后重试加载文件首先检查是否

    [NSFileVersion unresolvedConflictVersionsOfItemAtURL:]

    有任何冲突,解决它们,重试打开文件,

    [NSFileManager evictUbiquitousItemAtURL:]

    [NSFileManager startDownloadingUbiquitousItemAtURL:]

    如果还是打不开,下载后重试。

    【讨论】:

      猜你喜欢
      • 2012-04-18
      • 2012-05-20
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      相关资源
      最近更新 更多