Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 753
n.evermind Avatar asked Oct 10 '22 13:10

n.evermind


1 Answers

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.

like image 138
valexa Avatar answered Oct 13 '22 11:10

valexa