Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to have an NSFetchedResultsController delete an object if one of the object's properties doesn't fulfil it's predicate?
    text
    copied!<p>I have an <code>NSFetchedResultsController</code>-backed <code>UICollectionViewController</code> that is populated by a list of objects. Each object has a BOOL property called deleted. My fetch controller uses the following predicate to filter my objects.</p> <pre><code>[NSPredicate predicateWithFormat:@"deleted == NO"]; </code></pre> <p>My issue is that when I delete the object by marking it as <code>deleted = @YES</code>. The subsequent <code>didChangeObject:</code> method tells me that the object was <em>updated</em> and <strong>NOT</strong> <em>deleted</em>. And the object does not get removed from the collection view. If I quit and reload my app, the object does not show up in the collection view, which is the correct behaviour.</p> <p>Is there something that I am doing wrong, or is this the expected behaviour of <code>NSFetchedResultsController</code>?</p> <p><strong>Update as requested here is the code:</strong></p> <p>Fetch controller configuration:</p> <pre><code>NSPredicate *predicate = [NSPredicate predicateWithFormat:@"deleted == %@", @NO]; NSSortDescriptor *timestampSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO]; NSSortDescriptor *prioritySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"priority" ascending:NO]; NSManagedObjectContext *managedObjectContext = [NSManagedObjectContext MR_defaultContext]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:[GYNotification description] inManagedObjectContext:managedObjectContext]; [request setEntity:entity]; [request setIncludesSubentities:YES]; [request setSortDescriptors:sortDescriptors]; [request setPredicate:predicate]; NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:sectionNameKeyPath cacheName:nil]; </code></pre> <p>Then the fetch controller delegate methods:</p> <pre><code>- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { NSMutableDictionary *change = [NSMutableDictionary new]; switch(type) { case NSFetchedResultsChangeInsert: change[@(type)] = newIndexPath; break; case NSFetchedResultsChangeDelete: change[@(type)] = indexPath; break; case NSFetchedResultsChangeUpdate: change[@(type)] = indexPath; break; case NSFetchedResultsChangeMove: change[@(type)] = @[indexPath, newIndexPath]; break; } [_objectChanges addObject:change]; } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { if ([_objectChanges count] &gt; 0) { [self.collectionView performBatchUpdates:^{ for (NSDictionary *change in _objectChanges) { [change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) { NSFetchedResultsChangeType type = [key unsignedIntegerValue]; switch (type) { case NSFetchedResultsChangeInsert: [self.collectionView insertItemsAtIndexPaths:@[obj]]; break; case NSFetchedResultsChangeDelete: [self.collectionView deleteItemsAtIndexPaths:@[obj]]; break; case NSFetchedResultsChangeUpdate: [self.collectionView reloadItemsAtIndexPaths:@[obj]]; break; case NSFetchedResultsChangeMove: [self.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]]; break; } }]; } } completion:nil]; } [_objectChanges removeAllObjects]; } </code></pre>
 

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