Question

I am trying to use iCloud to store my app's userSetting, here is my Save & Load Code: , it usually doing fine but sometimes crash with message like: attempt to open or a revert document that already has an open or revert operation in flight or send to dealloc instance so i add fileState logs befere openWithCompletionHandler it always show state = UIDocumentStateClosed no matter will crash or not, i save data when applecationDidEnterBackground and load when applicationDidBecomeActive.

save:

-(void)storeToiCloud{
    NSURL *baseURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    if (baseURL) {
        NSURL *documentsURL = [baseURL URLByAppendingPathComponent:@"Documents"];
        NSURL *documentURL = [documentsURL URLByAppendingPathComponent:[NSString stringWithFormat:@"userSetting"]];
        if (!loadDocument) {
            self.loadDocument = [[MyUserDefaultsDocument alloc] initWithFileURL:documentURL];
        }
        loadDocument.myUserDefault = [MyUserDefaults standardUserDefaults];

        [loadDocument saveToURL:documentURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
        }];
    }
}

load:

-(BOOL)shouldSynciCloud{
    if (![Utility iCloudEnable]) {
        return NO;
    }
    NSURL *baseURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    if (baseURL) {
        self.query = [[[NSMetadataQuery alloc] init] autorelease];
        [self.query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == 'userSetting'", NSMetadataItemFSNameKey];
        [self.query setPredicate:predicate];

        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self selector:@selector(queryDidFinish:) name:NSMetadataQueryDidFinishGatheringNotification object:self.query];

        [self.query startQuery];
        [Utility showSpinner];
        return YES;
    }
    return NO;
}

- (void)queryDidFinish:(NSNotification *)notification {
    NSMetadataQuery *query = [notification object];

    // Stop Updates
    [query disableUpdates];
    // Stop Query
    [query stopQuery];
    [query.results enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        NSURL *documentURL = [(NSMetadataItem *)obj valueForAttribute:NSMetadataItemURLKey];

        if([[documentURL lastPathComponent] hasPrefix:@"userSetting"]){
            self.document = [[MyUserDefaultsDocument alloc] initWithFileURL:documentURL];
            NSString* message;
            if (document.documentState == UIDocumentStateNormal){
                message = @"UIDocumentStateNormal";
            }else if (document.documentState == UIDocumentStateClosed) {
                message = @"UIDocumentStateClosed";
            }else if(document.documentState == UIDocumentStateEditingDisabled){
                message = @"UIDocumentStateEditingDisabled";
            }else if(document.documentState == UIDocumentStateInConflict){
                message = @"UIDocumentStateInConflict";
            }else if(document.documentState == UIDocumentStateSavingError){
                message = @"UIDocumentStateSavingError";
            }
            NSLog(@"state = %@",message);
            [document openWithCompletionHandler:^(BOOL success) {
                if (success) {
                    MyUserDefaults *prefs = [MyUserDefaults standardUserDefaults];                    
                    NSData *book =[document.myUserDefault.realDict objectForKey:@"realbook"];
                    NSData *readSetting = [document.myUserDefault.realDict objectForKey:@"epubRS"];

                    if (book&&[[NSUserDefaults standardUserDefaults] boolForKey:@"iCloudBook"]) {
                        [prefs setObject:book forKey:@"realbook"];
                        [Utility reloadRealBooks];
                    }
                    if (readSetting&&[[NSUserDefaults standardUserDefaults] boolForKey:@"iCloudSetting"]) {
                        [prefs setObject:readSetting forKey:@"epubRS"];
                        [Utility setEpubReadSettingFromData:readSetting];
                    }
                    [prefs save];

                    [[NSNotificationCenter defaultCenter]postNotificationName:@"iCloudSynced" object:nil];
                    [Utility removeSpinner];
                }
                else{
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"iCloudSyncfailed" object:nil];
                    [Utility removeSpinner];
                }
            }];
        }
    }];
    if ([query.results count]==0) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"iCloudSyncfailed" object:nil];
        [Utility removeSpinner];
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:nil];
}
Was it helpful?

Solution

As noted in this question, the error occurs if your app attempts to call [document openWithCompletionHandler:] method twice in close succession.

Because the openWithCompletionHandler: opens the document asynchronously, the document may still be opening when the method is called again.

If this happens, your app ends up trying to open the document twice (as the document state will remain UIDocumentStateClosed until completion) and this causes the exception to be thrown.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top