title: Core Data: -deleteObject: Crashes, Are Delete Rules the Cause?
body: <p>I have the following model, as you can see in the image.</p>
  
  <p><a href="http://img521.imageshack.us/img521/9741/schermata20100224a12251.png" rel="nofollow noreferrer">alt text http://img521.imageshack.us/img521/9741/schermata20100224a12251.png</a></p>
  
  <p>My application requires refreshing every instance of B, so at each <code>viewWillAppear</code>, I need to  delete all the B's in the model. Upon deleting a B, the cascade delete rule on the relationship to C will delete all C and then cascade to all D.
  A &amp; E are constants.</p>
  
  <p>I have the DeleteRule on each object as follows:</p>
  
  <pre><code>A: b - Cascade
  B: c - Cascade, a - Nullify
  C: b - Nullify, d - Cascade
  D: c - Nullify, e - Nullify
  E: d - Cascade
  </code></pre>
  
  <p>or </p>
  
  <pre><code>A -(cascade)-&gt;&gt; B -(cascade)-&gt; C -(cascade)-&gt;&gt; D -(nullify)-&gt; E
  A &lt;-(nullify)- B &lt;-(nullify)- C &lt;-(nullify)- D &lt;-(nullify) E
  </code></pre>
  
  <p>I am having problem with cascading the delete on all B,C,D. My fetchRequest object returns every instance of B in A and then I call the <code>-deleteObject:</code> on each B from the managedObjectContext. But <strong>there is the EXC_BAD_ACCESS on the call to [managedObjectContext save:&amp;error]</strong>.</p>
  
  <p>Can someone show me what I am doing wrong? Am I having problems with the DeleteRule on each entity or does the problem lay elsewhere? What are the best practices to handle the cascading deletes on three objects B,C,D?</p>
  
  <p><strong>Edited:</strong></p>
  
  <p>Here is the stack trace when the error is raised:</p>
  
  <pre><code>  #0    0x01d843ae in ___forwarding___
    #1    0x01d606c2 in __forwarding_prep_0___
    #2    0x01c618b6 in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]
    #3    0x0003263a in _nsnote_callback
    #4    0x01d4f005 in _CFXNotificationPostNotification
    #5    0x0002fef0 in -[NSNotificationCenter postNotificationName:object:userInfo:]
    #6    0x01bc217d in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:]
    #7    0x01c21763 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:]
    #8    0x01ba65ea in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:]
    #9    0x01bdc728 in -[NSManagedObjectContext save:]
  </code></pre>
  
  <p>Here is the log in the console when I tried to set NSZombieEnabled &amp; MallocStackLogging to YES:</p>
  
  <pre><code>  2010-02-24 15:41:39.803 Foo[2591:207] deleting object: FUM5
    2010-02-24 15:41:40.515 Foo[2591:207] *** -[viewController controllerWillChangeContent:]: message sent to deallocated instance 0x7e54510
  </code></pre>
  
  <p><strong>Edit 2: SOURCE CODE ADDED</strong></p>
  
  <p>I have tried to recreate the situation by creating a new project with the exact schema in the image. You can <a href="http://www.2shared.com/file/11640639/4e825b57/CoreDataTestDeleteRule.html" rel="nofollow noreferrer">download it from here</a>. There is a README text as well. Hope that I have given enough information.</p>