NSFetchedResultsController updating with wrong upd

2020-03-26 04:50发布

问题:

Please see the comments before blindly voting this as a duplicate. It isn't a duplicate question.

First - I'm using MagicalRecord for core data. Second - My UICollectionView is populated with an NSFetchedResultsController.

The NSPredicate for the NSFetchedResultsController is ...

NSPredicate *eventPredicate = [NSPredicate predicateWithFormat:@"event = %@", self.event];
NSPredicate *deletedPredicate = [NSPredicate predicateWithFormat:@"deleted == NO"];

NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[eventPredicate, deletedPredicate]];

When I add new items to CoreData using Magical Record's saveWithBlock the collectionView updates properly and animates the changes into view.

The problem is that when I change the value of deleted to @YES...

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
    Photo *photo = [[self photoAtIndexPath:[self.collectionView indexPathForCell:cell]] inContext:localContext];
    photo.deleted = @YES;
}];

Then the NSFetchedResultsController delegate methods fire except it fires with the change type NSFetchedResultsChangeUpdate. This isn't what it should be though. It should be NSFetchedResultsChangeDelete as changing the value of deleted should remove it from the fetch request.

I don't want to just delete the object as I need to update a server with the deleted ID.

Also, if I then pop the view and push it back again the photo is gone so I know it is updating it properly and I know it's removing it from the fetch request.

Any ideas?

回答1:

You must not call an attribute "deleted", because that conflicts with the isDeleted method of NSManagedObject.

See Core Data NSPredicate "deleted == NO" does not work as expected or NSPredicate does not get executed on first launch for different examples that "strange things" happen if an attribute is called "deleted".