Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Check out "delete propagation". It's there to solve exactly that problem.</p> <p>If that doesn't do exactly what you want / need: You can override <code>- (void)prepareForDeletion</code> on the Book entity and at that point check for any Authors that are registered with the context and have pending changes (since their inverse will have changed) and have no books:</p> <pre><code>{ // ... [[NSNotificationCenter defaultNotificationCenter] addObserver:self selector:@selector(deleteOrphanedAuthors:) name:NSManagedObjectContext object:moc]; // ... } - (void)deleteOrphanedAuthors:(NSNotification *)note; { NSManagedObjectContext *moc = [note object]; NSManagedObjectModel *mom = [[moc persistentStoreCoordinator] managedObjectModel]; NSEntityDescription *authorEntity = [[mom entitiesByName] objectForKey:@"Author"]; for (NSManagedObject *author in [moc updatedObjects]) { if ([author entity] == authorEntity) { if (![author hasFaultForRelationshipNamed:@"books"] &amp;&amp; ([[author books] count] == 0)) { [moc deleteObject:author]; } } } } </code></pre> <p><strong>Note:</strong> You can <strong>not</strong> pass <code>nil</code> as the object (i.e. context) to observe, since frameworks you use, might have their own context, and you do not want to mess with them.</p> <p>Also, note how this code is careful not to touch the <code>author</code> object if it's a fault. If a book is deleted, Core Data will change the corresponding author objects' inverse relationships, hence fault in that relationship, such that it is no longer a fault. And the code will only operate on those objects.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload