mkc842
mkc842

Reputation: 2381

core data MOC save pauses execution and fails to save (w/o error or crash)

I have recently implemented core data concurrency and thread confinement. I am working out the wrinkles.

I have a main MOC and a private MOC, each linked to the PSC nonhierarchically. They are merged whenever one is saved.

I am having an issue where a MOC save fails. The app does not crash, but the object does not save and execution pauses (but can be resumed) with the following displayed in the Issue Navigator:


As you can see, this is executing in the private MOC queue, which is correct, because here's the line where execution pauses:


As you can see, I am sending the save message to the private MOC while in the private MOC queue.

Any ideas on how to troubleshoot this? Because I get no error message, I really don't know where to start. Thanks!

Update

I replaced my save/error code as Marcus instructed. It now reads:

[[self privateManagedObjectContext] performBlockAndWait:^{
    NSError *error = nil;
    if (![[self privateManagedObjectContext] save:&error]) {
        NSLog(@"ERROR SAVING MOC IN syncParseObjectLocally: %@\n%@", [error localizedDescription], [error userInfo]);
    }
    NSLog(@"%@", error);
    NSLog(@"%@\n%@", [error localizedDescription], [error userInfo]);
}];

In the log, that yields all null.

I am not using iCloud.

When I turn off the all-(objective-c-)exceptions breakpoint, execution continues seamlessly (so I can probably ignore this hiccup, although more importantly, I need to address this likely related problem: core data changes don't merge)

When I enter 'po error' manually when the breakpoint triggers, the log shows:

(lldb) po error
 nil

Upvotes: 0

Views: 367

Answers (1)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

You are probably encountering one of the internal exceptions. Core Data will throw an exception internally whenever there is a potential merge conflict. It may resolve the merge (which is what you are seeing) but it will still throw.

To test this, change your breakpoint to only break on Objective-C exceptions and not C++ exceptions. If you don't see the break anymore then that is what you were seeing.

Also, file a radar; this exception is annoying and I would love it if we got enough votes in to get it looked at.

Update

Is there any console output?

Is there any value in the error variable?

I know you stated that you get no error message but I want to validate that the error variable is nil vs. having no payload. Can you do a po error at that breakpoint?

Also, I would try changing that line of code to:

[[self privateManagedObjectContext] performBlockAndWait:^{
  NSError *error = nil;
  if (![[self privateManagedObjectContext] save:&error]) {
    NSLog(@"ERROR SAVING MOC IN syncParseObjectLocally: %@\n%@", [error localizedDescription], [error userInfo]);
  }
}];

Just to test that the queue really belongs to this NSManagedObjectContext.

Update

Are you using iCloud?

Does the application run with no issues if you turn breakpoints off?

When I referred to po error I meant to do it when the breakpoint was hit and to type it in manually.

I trust calling -performBlockAndWait: had no effect either.

I am wondering if you have found another new internal exception being thrown in Core Data and I am hoping we can isolate what causes it to trigger.

Upvotes: 2

Related Questions