NSFileCoordinator error when using UIManagedDocument in iOS 5.0 simulator

1.6k views Asked by At

I am using a UIManagedDocument in iOS 5.0, running the app on the simulator, using XCode 4.2 under OSX 10.6. The code in question looks as follows:

if (![[NSFileManager defaultManager] fileExistsAtPath:[self.photoDatabase.fileURL path]]) {
    // does not exist on disk, so create it
    [self.photoDatabase saveToURL:self.photoDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
        [self setupFetchedResultsController];
        [self fetchFlickrDataIntoDocument:self.photoDatabase];

    }];
} else if (self.photoDatabase.documentState == UIDocumentStateClosed) {
    // exists on disk, but we need to open it
    // *** the following line generates the message ***
    [self.photoDatabase openWithCompletionHandler:^(BOOL success) {
        //[self setupFetchedResultsController];
        }];
} else if (self.photoDatabase.documentState == UIDocumentStateNormal) {
    // already open and ready to use
    [self setupFetchedResultsController];
}

Running the marked line creates the following message on the log:

2012-01-10 22:33:17.109 Photomania[5149:4803] NSFileCoordinator: A surprising server error was signaled. Details: Connection invalid

After the message is sent, the UIManagedDocument may or may not work—I have not found the circumstances that determine this, yet.

I am pretty sure that the code is correct, as it's actually one of the code examples in the CS193p course from Stanford. The whole example can be downloaded at their website under http://www.stanford.edu/class/cs193p/cgi-bin/drupal/ Direct link to the code: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/system/files/sample_code/Photomania_0.zip

Additionally, the code runs fine on the device itself, without generating the "surprising" message, and running all the code that comes afterwards just fine.

I have not found anything on Google, neither on the Apple Developer pages. Restarting the simulator, or XCode, or reinstalling both of them does not change the behaviour.

Any ideas?

5

There are 5 answers

0
Jody Hagins On

I love the Stanford iTunes class. However, I think the sample code for using UIManagedDocument is wrong. In fact, he notes in the demo that he is only doing it that way because he wants to just fetch the information right then. In the code comments, he says not to use the auto-save features because the data will not be saved if the app quits. however, UIManagedDocument will save anything that's necessary before quitting. It has all pertinent handlers for quitting/multitasking/etc to make sure the data is saved.

So, if you are using that code as your example, here's a version that should work, and does not use saveToURL (I don't have a flickr account, so I didn't actually run it - but this is how the class is designed to work). Please let me know if it does not work.

- (void)fetchFlickrDataIntoDocument:(UIManagedDocument *)document
{
    NSManagedObjectContext *ctx = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSPrivateQueueConcurrencyType];
    ctx.parentContext = document.managedObjectContext;
    [ctx performBlock:^{
        NSArray *photos = [FlickrFetcher recentGeoreferencedPhotos];
        for (NSDictionary *flickrInfo in photos) {
            [Photo photoWithFlickrInfo:flickrInfo inManagedObjectContext:ctx];
            // Push changes to document MOC
            [ctx save:0]; // propagates changes to parent MOC
            // and tell the document it is dirty and needs to be saved
            // It will be saved when the document decides its time to save
            // but it *will* be saved.
            [document updateChangeCount:UIDocumentChangeDone]
        }
    }];
}
0
shazgoose On

Still had errors when the last path component for document file url was @"Database". Adding an extension @"Database.db" seems to have fixed it, everything running fine now. Have also upgraded to Lion though.

NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Database.db"];  
0
Daniel On

Try upgrading to the latest iOS 5.1. I don't think UIManagedDocument with iCloud works reliably in 5.0. This has been my experience.

0
Jeff On

I can only say that I've had this happen to me several times. For me, I'm lazy after I update my dataModel and so far, each time I've gotten this error it was because I had changed my data model. Usually, all I need to do is delete my app from the simulator and re-run it and it has always turned out fine. Hope this helps someone out there.

0
LNI On

I think I have found the answer. It looks like the automatic saving for UIManagedDocument kicks in only after a few seconds on the simulator.

So I minimized the app on the simulator, by pressing the home button, and then clicked on the icon to maximize it again. And then I terminated the app in simulator.

When I re-launched the app, the database was loaded. The error still shows up - it comes because the document is in "closed" state (that's normal - that's why CS193P asked to call openWithCompletionHandler), but my data across launches is preserved. Unfortunately I have to do the minimize/maximize routine before terminating the app, or the changes are discarded at next launch.

Can you verify that this is the behavior you are able to recreate? At least for testing purposes this should be a good enough trick to use.