views:

70

answers:

1

I am sorting the UITableView by using a predicate with NSFetchedResultsController. But, when I make a change in a detail view and save it, the object I changed immediately gets placed in its new location in the UITableView.

I have up/down buttons similar to the message view in Mail. This behavior disrupts the ordering of items, and I'd like to delay this change until the user backs out to the UITableView for UX reasons. I implemented the NSFetchedResultsControllerDelegate methods in the UITableViewController. Is there an easy/clever way to do this?

Edit: Below are the NSFetchedResultsControllerDelegate methods implemented in the UITableViewController.

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;

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

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath]
                    atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}
+2  A: 

The problem is not in the NSFetchedResultsControllerDelegate methods. Those methods only come into play only if something changes the data model while the table is visible.

Your problem is that you change the data while the table view is off screen and inactive. Tableviews are designed to automatically display the data model given to them. When you return to the tableview from the detail view, it automatically reloads its data which now contains the changes made in the detail view. It would do that regardless of how you provided data to the table.

The only way to prevent the table from displaying the changes until you want it to is to not add the data to the data model until you want the change to appear. You would have to pass the data from the detail view back to the table in a non-managedObject and create the managedObject in the tableview controller when you wanted it to appear.

However, from a UI design perspective, I suggest you rethink your design. If you don't change the table until after it reloads, how will you signal the users that you are going to make the change? Will the UI just suddenly update for no apparent reason? Will the user have to initiate it? What if they forget?

I think that users will expect any changes made in the detail view to be instantly reflected in the tableview because that is how virtually all tableview-detailView pairings work. For example, if you change a contact's name in the contact detail of the AddressBook that is reflected instantly when you go back to list of contacts.

TechZen
Thanks. I was considering making a staging area similar to what you said, and pass the staged changes at the right time.As for the UI perspective, the table is sorted by rating for each item. When the rating changes, it completely changes your place in the list, so using the next/prev buttons before and after changing a rating will give different results. Also, I like being able to handle the use case where the user being able to go through all the unrated items to rate them without losing his place. I'm not completely convinced either design is completely correct. What do you think?
Simon
If I understand your design, I think you might want to add an undisplayed rating to your data model which you use to sort the table. When the user makes a change, you change the displayed rating but not the undisplayed rating so the object remains in the same UI order it had before the change. Then you could have a "sort table" button that would copy the displayed rating to the undisplayed rating and put everything in the proper order.
TechZen
Ah, that makes a lot of sense. Thanks!
Simon