NSFetchedResultsController removed from UITableView when updating

Please be careful, this is my first post.

When I update the NSManagedObject subclass and save using the selected result controller, instead of calling NSFetchedResultsChangeUpdate in "didChangeObject", it calls NSFetchedResultsChangeDelete. Saving works and updates the objects, but then immediately removes it from the UITableView. If I leave and return, the object will appear again in the update table view, so I know that it saves it in the database.

I am developing code for a simple task application using XCODE 5. I am using the iPhone 4S, not a simulator, to test it. It worked fine until I made a few mods in another part of the code (I thought it was not connected, and now it does not work.

I have a sort order, and when I update the object and it changes in sort order, there was a good animation to move the UITableViewCells .... now I need to force the selection again and make [self. tableView reloadData]. This is a hack since I don't get the animation, but this is the only way I can update it:

I have preparation for the segue method:

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
// configure the destination view controller:
if ( [segue.destinationViewController isKindOfClass: [ShowEditTaskTableViewController class]])
{

    if ([sender isKindOfClass:[UITableViewCell class]] )
    {


        NSIndexPath *indexPath = [self.taskTableView indexPathForCell:sender];
        [self.taskTableView deselectRowAtIndexPath:indexPath animated:YES];

        // Pass the selected task to the new view controller.
        ShowEditTaskTableViewController *detailViewController = segue.destinationViewController;

        Task *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
        detailViewController.editedObject = info;
    }
    else
    {

        NSIndexPath *indexPath = [self.taskTableView indexPathForCell:sender];
        [self.taskTableView deselectRowAtIndexPath:indexPath animated:YES];
        ShowEditTaskTableViewController *detailViewController =           segue.destinationViewController;

        Task *info = (Task *)[NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:self.managedObjectContext];
        detailViewController.editedObject = info;




    }

}

}

There is nothing special here. "Task" is my NSManagedObject.

I have rewind (EDIT: change back to rewind after comment) segue and before calling it I set the variables that will be set in the editObject file .:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

if ([segue.identifier isEqualToString:@"addNewTaskSave"]) {
    // Note: This is an unwind segue to go back to the previous screen.

    self.dueDate = self.taskDatePicker.date;
    self.description = self.taskDescriptionTextView.text;
    self.priority = self.taskPrioritySegment.selectedSegmentIndex;
    }
}

and in RootViewController it calls:

- (IBAction)addNewTaskSave:(UIStoryboardSegue *)segue
{

ShowEditTaskTableViewController *controller = segue.sourceViewController;

controller.editedObject.shortDesc = controller.description;
controller.editedObject.priority = @(controller.priority);

controller.editedObject.dueDate = controller.dueDate;
controller.editedObject.completed = @0;

NSError *error;
if (![self.managedObjectContext save:&error]) {
    ...
}

.....}

It uses standard delegation methods didChangeObject. This worked fine until I changed anything.

NSFetchedResultsChangeDelete .

, :

self.fetchedResultsController = nil;

if (![[self fetchedResultsController] performFetch:&error]) {
   ...
}
[self.taskTableView reloadData];

, View .

. , .

SO, 4 (, IOS), - .

.

:

, controller.editedObject isDeleted = NO, isUpdated = YES, , NSFetchedResultsControllerChangeDelete.

+4
2

, SO . . fooobar.com/questions/1307043/...

UISegmentControl int .

 int index = self.filterSegment.selectedSegmentIndex;
 NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"filter == %d", index]];
 [fetchRequest setPredicate:predicate];

"filter" NSManagedObject NSNumber. NSPredicate

 NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"filter == %@", @(index)]];

.

0

; - .

/ . , prepareForSegue:sender: . , , ( "exit" ).

, , "" , . , push segue , "pop" segue.

, . , segue A B, B A. , , . , segues.

, . , , , , "" . NSFetchedResultsControllerDelegate " ".

+1

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


All Articles