Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8522717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:12:15+00:00 2026-06-11T07:12:15+00:00

I am trying to use iCloud to store my app’s userSetting, here is my

  • 0

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];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:12:16+00:00Added an answer on June 11, 2026 at 7:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use a Java Uploader in a ROR app (for its ease
I am need paint my image. I'm trying use JQuery in here this link:
trying to use hibernate with my web app and getting following exception: Initial SessionFactory
I'm a little confused here, I am trying use a partial view in a
Trying to use this sql statement. The first 2 parts work fine, I am
i'm trying use webview to load a image from sdcard i use this path
I am trying use TinyMCE to get code syntax to be displayed in the
I am trying use cursor for pagination. Forwarding pagination work fine, but I can
I'm trying use regular expression to convert special characters in an url. Here's my
I have a simple reader app on the app store that I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.