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?
- Init the instance
- Save it for creating
- Open it (to add my initial items to it)
- Make my additions
- 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.