iCloud's loadFromContents - how to deal with UIDocumentStateSavingError & UIDocumentStateEditingDisabled

时光毁灭记忆、已成空白 提交于 2019-12-06 03:13:06

问题


I'm using iCloud in my app to load text files. When loading text files, this method is called by iCloud when I call _UIDocument openWithCompletionHandler:^(BOOL success) etc:

    -(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;
}

Now the big problem is when documentState is 8 (UIDocumentStateEditingDisabled) or 12 (UIDocumentStateSavingError & UIDocumentStateEditingDisabled). This will usually lead to a crash of the app. I tried to return NO if the documentState is > 7, i.e. if it is either 8 or 12 but this results in not loading any contents at all.

I guess the problem is that the UIDocument won't load anything into self.books if it editing is disabled or if there was a saving error.

What would be a good practice to handle such errors? Also, why didn't Apple suggest in their sample code to check the documentState before loading data into UIDocument (iCloud Docs)? I guess that I'm doing something fundamentally wrong.


回答1:


Do you have conflict management implemented ?

In those scenarios you should attempt a number of things then retry loading the file the first being to check if

[NSFileVersion unresolvedConflictVersionsOfItemAtURL:]

has any conflicts, resolve them, retry opening the file,

[NSFileManager evictUbiquitousItemAtURL:]

and

[NSFileManager startDownloadingUbiquitousItemAtURL:]

if still can not open it, retry again after it downloaded.



来源:https://stackoverflow.com/questions/8057019/iclouds-loadfromcontents-how-to-deal-with-uidocumentstatesavingerror-uidocu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!