1

This is my object model:

A Client can have many Projects (optional), but a Project must have only one Client (required). A MemoEntry can have only one Project (optional), and there can be different MemoEntry(s) with the same Project (optional).

object model

Upon Delete:

  • Client.projects has a Deny Rule
  • Project.client has a Nullify Rule
  • Project.memos has a Deny Rule
  • MemoEntry.project has a Nullify Rule

Code that is called upon Delete of Client, Project or MemoEntry inside each of their own TableViewControllers:

// Delete the managed object.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

NSError *error;
if (![context save:&error]) {
  // Update to handle the error appropriately.
  NSLog(@"MyApp - Unresolved error %@, %@", error, [error userInfo]);
  exit(-1);  // Fail
}

Problem in bold:

If I create a Project and then delete it's parent Client, the code correctly follows into the IF statement. If I access the "deleted" Client, it is still there and I can edit clientName and other properties, save it and all fine. I can go to the Projects and open the Project and see the updated Client's clientName. But when I access the Client via the Project, my app exits.

Are my delete rules not set up properly? Does calling [context save:&error] saves the delete, even if it goes on error? Any thoughts?

*edit - Line where breakpoint error stops:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:passedManagedObjectContext];

Error thrown:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Client''
*** Call stack at first throw:
(
    0   CoreFoundation                      0x30897ed3 __exceptionPreprocess + 114
    1   libobjc.A.dylib                     0x3002f811 objc_exception_throw + 24
    2   CoreData                            0x3162f575 +[NSEntityDescription entityForName:inManagedObjectContext:] + 124
    3   EasyMemo                            0x00008ebb -[EditingViewController viewWillAppear:] + 1478
    4   UIKit                               0x31ec4d9b -[UINavigationController _startTransition:fromViewController:toViewController:] + 610
    5   UIKit                               0x31ec4ac3 -[UINavigationController _startDeferredTransitionIfNeeded] + 182
    6   UIKit                               0x31ebd21b -[UINavigationController pushViewController:transition:forceImmediate:] + 606
    7   UIKit                               0x31ebcfb3 -[UINavigationController pushViewController:animated:] + 34
    8   EasyMemo                            0x000058fd -[DetailProjectViewController tableView:didSelectRowAtIndexPath:] + 368
    9   UIKit                               0x31f7ae6f -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 662
    10  UIKit                               0x31f772af -[UITableView _userSelectRowAtIndexPath:] + 130
    11  Foundation                          0x349c7e8d __NSFireDelayedPerform + 368
    12  CoreFoundation                      0x3084e7fb __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
    13  CoreFoundation                      0x3084e2ad __CFRunLoopDoTimer + 860
    14  CoreFoundation                      0x3081f7a5 __CFRunLoopRun + 1088
    15  CoreFoundation                      0x3081f277 CFRunLoopRunSpecific + 230
    16  CoreFoundation                      0x3081f17f CFRunLoopRunInMode + 58
    17  GraphicsServices                    0x31e445f3 GSEventRunModal + 114
    18  GraphicsServices                    0x31e4469f GSEventRun + 62
    19  UIKit                               0x31e51123 -[UIApplication _run] + 402
    20  UIKit                               0x31e4f12f UIApplicationMain + 670
    21  EasyMemo                            0x0000259f main + 70
    22  EasyMemo                            0x00002554 start + 40
)

terminate called after throwing an instance of 'NSException'
elcool
  • 6,041
  • 7
  • 29
  • 44

2 Answers2

0

what is the object being deleted that is contained in your NSFetchedResultsController?

If -save: fails then nothing is written out to the persistent store and your objects are not deleted. This is why you can still access them.

As for the "But when I access the Client via the Project, my app exits."

What is the error?

What is being reported in the console?

Set a breakpoint on objc_exception_throw and see what is actually causing the crash.

Community
  • 1
  • 1
Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • Ok, I set up the breakpoint, edited my post with the line where it stopped. The object that was deleted is a Client. So if I load the Project and access the Client to display it, it goes on error. – elcool Nov 10 '10 at 04:20
0

Found the problem:

I was using the Books example for adding. And I was passing the addingManagedObjectContext they have when just calling the objects without adding. So it was empty...

elcool
  • 6,041
  • 7
  • 29
  • 44