title: Delete Rule Cascade on Core Data 3 entities relation
body: <p>I have a relation with 3 entities in my Core Data model with the next relation A&lt;&lt;---->B&lt;&lt;---->C.</p>
  
  <p>When A object is deleted B must be deleted if there is no more A in the A&lt;&lt;--->B relation, and C must be deleted if there is no more B in the B&lt;&lt;--->C relation.</p>
  
  <p>I tried to configure the relation with:</p>
  
  <p>First option:</p>
  
  <ul>
  <li>A to B relation is "to one" and Deletion Rule is "Cascade"</li>
  <li>B to A relation is "to many" and Deletion Rule is "Nullify"</li>
  <li>B to C relation is "to one" and Deletion Rule is "Cascade"</li>
  <li>C to B relation is "to many" and Deletion Rule is "Nullify"
  In this case when I delete an A entity B and C are always deleted no matter there are more entities.</li>
  </ul>
  
  <p>Second option:</p>
  
  <ul>
  <li>A to B relation is "to one" and Deletion Rule is "Cascade"</li>
  <li>B to A relation is "to many" and Deletion Rule is "Deny"</li>
  <li>B to C relation is "to one" and Deletion Rule is "Cascade"</li>
  <li>C to B relation is "to many" and Deletion Rule is "Deny"
  In this case there is a exception and there is no action.</li>
  </ul>
  
  <p>Third option:</p>
  
  <p>I tried to use prepareForDeletion with this example code in B:</p>
  
  <pre><code>- (void) prepareForDeletion {
      [super prepareForDeletion];
  
      EntityC *entityC = self.entityC;
      if (entityC &amp;&amp; (entityC.isDeleted == NO)) {
          NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isDeleted == NO"];
          NSSet *setOfBs = [entityC.Bs filteredSetUsingPredicate:predicate];
  
          if ([setOfBs count] == 0) {
              [self.managedObjectContext deleteObject:entityC];
          }
      }
  </code></pre>
  
  <p>In this last case I have a random result. Sometimes it works as I expect and sometimes I only delete A entity.</p>
  
  <p>Is there any simple way to solve this problem. I'd prefer to use a solution like First or Second one, but I think is only possible using a solution like the third one.</p>