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 8551733
In Process

The Archive Base Latest Questions

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

I’ve been reading everything I can here about UIDocument and UIManagedDocument since there is

  • 0

I’ve been reading everything I can here about UIDocument and UIManagedDocument since there is more help on UIManagedDocument and it’s a subclass of UIDocument, but I’ve not been able to figure what I need to do about this.

When the user hits the add button for my app to create a new document I need to create the document and have a couple of items prepopulated into it. My code for doing this works fine as long as I am single-stepping through the debugger, but at full speed the couple of items don’t make it into the document.

I store a reference to the document in my AppDelegate and have a macro defined for simplified reading in the code:

#define SelectedDocument [(AppDelegate *)[[UIApplication sharedApplication] delegate] selectedDocument]

So in handling the add request, my code does this:

GnKDocument *tmp = [[GnKDocument alloc] initWithFileURL:fileURL];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setSelectedDocument:tmp];
[SelectedDocument saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
    if (success) {
        [SelectedDocument openWithCompletionHandler:^(BOOL success) {
            if (success) {
                [SelectedDocument setDateCreated:[NSDate date]];
                NSString *c1UUID = [SelectedDocument appendChapterWithName:@"Chapter 1"
                                                               withColor:kChapterColorYellow];
                NSString *p1c1UUID = [SelectedDocument appendPageWithParent:c1UUID
                                                                       withName:@"Page 1"
                                                                      withColor:kPageColorRed];
                NSLog(@"Just added Page 1 as %@ to chapter %@", p1c1UUID, c1UUID);
                [SelectedDocument closeWithCompletionHandler:^(BOOL success) {
                 }];
            }
     }
 }];

The two append calls into my UIDocument subclass do their work and then call

[self updateChangeCount:UIDocumentChangeDone];

And as a testing step I overrode that method just to log out that changes were being made:

- (void)updateChangeCount:(UIDocumentChangeKind)change
{
     [super updateChangeCount:change];
     NSLog(@"GnKDocument recording a change");
}

Am I doing things in the right order? Should I be dispatching calls off to various queues?

  1. Init the instance
  2. Save it for creating
  3. Open it (to add my initial items to it)
  4. Make my additions
  5. Close it (according to the docs, closeWithCompletionHandler: asynchronously saves any changes).

Again, if I set breakpoints at each call (saveToURL:, openWithCompletionHandler:, and closeWithCompletionHandler) and “step over” those calls, then run to get into the completion handlers, the document ends up on disk as I intended. If I disable my breakpoints and run again, the document is created on disk and the changes are logged, but the closed file does not contain my two initial elements.

  • 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-11T14:15:37+00:00Added an answer on June 11, 2026 at 2:15 pm

    For the longest time I thought the difference was the speed with which the code was executing was either creating or avoiding a race condition. But in investigating that possibility I added lots of NSLog statements everywhere something critical was happening… and the problem went away. So it clearly wasn’t a timing issue. Looking back over the contents of the NSLog statements I realized that some of the values were being lazy-loaded, and the act of referencing them in the NSLog statements was causing them to be loaded. Similarly, while single-stepping through the code, I suspect that “Print description of…” commands were having the same effect (maybe).

    So in my case, the UIDocument subclass builds a file wrapper that contains two files, one for metadata about the document and another that is the actual document data. There was a test to verify minimal correctness in the document, and this test used values from the metadata file which were meant to be lazy loaded but had never been accessed, so the test determined the file was not valid and set up the initial values all over again, wiping out the two items I was prepopulating the file with.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this

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.