I'm using Stanford's CoreDataTableViewController to display a dynamic table.
How the app works: To add a row to this table, a child screen is popped for data entry, and when the child screen closes, the newly created managedObject is inserted, and a callback reloads the table.
I've noticed that newly added rows are intermittently missing from the table upon re-entering the screen. I noticed this behaviour only occurs when the UIManagedDocument object is created for the first time using saveToURL. Subsequently, after restarting the app and opening the UIManagedDocument using openWithCompletionHandler, the list always displays correctly.
Here's the code I'm using to create/open the UIManagedDocument:
- (void)performWithDocument:(OnDocumentReady)onDocumentReady
{
void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success)
{
NSLog(@"Called OnDocumentDidLoad");
onDocumentReady(self.document);
};
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]])
{
[self.document saveToURL:self.document.fileURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:OnDocumentDidLoad];
}
else if (self.document.documentState == UIDocumentStateClosed)
{
NSLog(@"Calling openWithCompletionHandler:OnDocumentDidLoad");
[self.document openWithCompletionHandler:OnDocumentDidLoad];
}
else if (self.document.documentState == UIDocumentStateNormal)
{
OnDocumentDidLoad(YES);
}
}
When running the above code, onDocumentDidLoad() is always called, and the success flag is YES.
Any help would be most appreciated. Thanks in advance!
-- Edited the following code to show my method of creating, then closing and reopening the document in response to Jody - I still encountered the same issue. ---
- (void)performWithDocument:(OnDocumentReady)onDocumentReady
{
void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success)
{
NSLog(@"Called OnDocumentDidLoad success: %d", success);
onDocumentReady(self.document);
};
void (^OnDocumentDidClose)(BOOL) = ^(BOOL success)
{
NSLog(@"Called OnDocumentDidClose, openWithCompletionHandler success: %d", success);
[self.document openWithCompletionHandler:OnDocumentDidLoad];
};
void (^OnDocumentDidSave)(BOOL) = ^(BOOL success)
{
NSLog(@"Called OnDocumentDidSave success: %d", success);
if (self.document.documentState == UIDocumentStateClosed)
{
NSLog(@"Calling openWithCompletionHandler:OnDocumentDidLoad from onDocumentDidSave - was closed");
[self.document openWithCompletionHandler:OnDocumentDidLoad];
}
else if (self.document.documentState == UIDocumentStateNormal)
{
// TODO Close and reopen?
NSLog(@"Calling closeWithCompletionHandler:OnDocumentDidClose from onDocumentDidSave - was normal");
[self.document closeWithCompletionHandler:OnDocumentDidClose];
}
};
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]])
{
// Database doesn't exist, create it, then close and reopen in completion handler
[self.document saveToURL:self.document.fileURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:OnDocumentDidSave];
}
else if (self.document.documentState == UIDocumentStateClosed)
{
// Open existing database
NSLog(@"Calling openWithCompletionHandler:OnDocumentDidLoad");
[self.document openWithCompletionHandler:OnDocumentDidLoad];
}
else if (self.document.documentState == UIDocumentStateNormal)
{
// Already opened
OnDocumentDidLoad(YES);
}
}