1

Here is a snippet of relevant code:

NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"Demo Document"];
    UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url];    
if (document.documentState == UIDocumentStateClosed) {
            NSLog(@"file is closed");
            NSLog(@"%@",[url path]);
            NSDate *start = [NSDate date];
            [document openWithCompletionHandler:^(BOOL success) {
                if (success) {
                    NSLog(@"finished OPEN");
                    NSDate *methodFinish = [NSDate date];
                    NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:start];
                    NSLog(@"time = %f",executionTime);
                    self.managedObjectContext = document.managedObjectContext;
                }
            }];
        }

The time interval between "file is closed" and "finished open" was 16.6 seconds. Is this normal behavior? Bad coding? Or is it because I am running on a simulator?

I don't know if it would matter, but the file is barely 50KB...

Nathan H
  • 48,033
  • 60
  • 165
  • 247

2 Answers2

0

This is not normal behavior. Have you tried clearing all iCloud data using Xcode 5 under "Debug->iCloud->Delete iCloud Contents"? It takes a few minutes to be fully realized but starting with a clean slate might help.

If you have already done this have you looked at what else might be working with that document that has a temporary hold on it? Or is something else blocking the main thread? Because that block won't execute until the main thread's event queue comes back around.

dtrotzjr
  • 928
  • 5
  • 18
0

Believe it or not, this is normal. It isn't really anything to do with your code, but more or less how it is intended to work when you use autosave. This autosaving behavior is actually turned on/enabled by default ( you can also turn it off - but I would highly recommend not doing that unless you need to and know what you're doing heh).

Anyways, UIManagedDocument actually automatically saves your data to a document store for you by default and in the background no less - but the caveat to this is that it will do it whenever it can ( or better, whenever it wants to ). Realize that it does this in the background for you so that you don't have to worry about it ( its a feature I really love about it - but can also be a pain int he ass ).

It sounds like you're needing it to be saved because it isn't showing your data or you aren't seeing the results you would expect.

If that's the case, then I think you should look more into how to have it actually save your changes so that it can show up in your controllers. In reality, it really has very little to do with when UIManagedDocument saves anyways, and more about when your context is updated. If this is, indeed, the case you may find my answer to a different thread/question on this same subject helpful: Multiple UIManagedDocument For Read & Write

This may or may not be what you're after, but I believe it will get you on the right path at the very least.

Community
  • 1
  • 1
Jonathon Hibbard
  • 1,547
  • 13
  • 20