NSPredicate calls update to update to return NSFetchedResultsChangeDelete, not NSFetchedResultsChangeUpdate

I have a predicate inside - (NSFetchedResultsController *)fetchedResultsController standard way, starting with the CoreDataBook example.

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"state=%@ && date >= %@ && date < %@", @"1",fromDate,toDate]; [fetchRequest setPredicate:predicate]; 

This works great, but when editing an item, it returns with NSFetchedResultsChangeDelete not Update. When the main view returns, it lacks an element. If I restart the simulator, the deletion was not saved, and the correct editing result showed that the predicate was working correctly.

 case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; 

I can confirm the behavior only by commenting ONLY two lines of predicates, and then everything works as it should return correctly with the full set after editing and calling NSFetchedResultsChangeUpdate instead of NSFetchedResultsChangeDelete.

I read http://matteocaldari.it/2009/11/multiple-contexts-controllers-delegates-and-coredata-bug which reports similar behavior, but I did not find work around my problem.

+4
source share
4 answers

I also encounter this problem. The main reason is caused by setting the attribute value in the NSPredicate format string with the wrong type.

For this problem, there may be a state attribute of type Number, for example, Integer 32 . But the code above passed it NSString ( @"1" ), just change it to NSNumber ( @1 ), it will fix the problem.

Matthew Weiss finally fixed the problem using the fetch request template. I think that " state " is checked in a template that does not have a type problem. And only fromDate and toDate are passed subs.

+5
source

I'm sorry that some time has passed and I will release a product soon. The job was to make stored procedures in the selected object and use

 NSFetchRequest *fetch = [model fetchRequestFromTemplateWithName:@"myTemplate1" substitutionVariables:subs]; 

This eliminates the use of Predicate on the FetchedResultsController.

When I want to use a predicate, I just verify that I am using the local managedObjectContext file and pass the array itself to work elsewhere. NSArray *array = [moc executeFetchRequest:fetchRequest error:&error]; return array;

This is a safer bet and allows you to repeatedly view the same data.

+1
source

Are you using more than one NSManagedObjectContext ? The message in which you were connected used several instances of NSManagedObjectContext , which are the root of its problem. This is not a mistake in the NSFetchedResultsController , but a misunderstanding on how it works.

Regarding your problem; first question: do you have a gap in the case before the one you showed?

What does the editing code look like? You show the predicate, but not the code that edits the object you specify, fires NSFetchedResultsChangeDelete .

0
source

In my post, I am dealing with a known bug in the NSFetchedResultsController (see https://devforums.apple.com/message/139580#139580 ), which maps a pointer between two objects in two different contexts of managed objects, instead evaluating objectID so I think this is not your problem.

Is your object processed as deleted only if you change its date outside the interval specified in NSPredicate or for any other modification?

0
source

Source: https://habr.com/ru/post/1307043/


All Articles