开发者

Use NSFetchedResultsControllerDelegate to update TableView sections & rows after NSPredicate change

开发者 https://www.devze.com 2023-02-01 19:51 出处:网络
Is there a way to update a UITableView sections and rows after changing the fetchedResultsControllers NSpredicate (and executing the fetch)? I would like to use controllerWillChangeContent delegate me

Is there a way to update a UITableView sections and rows after changing the fetchedResultsControllers NSpredicate (and executing the fetch)? I would like to use controllerWillChangeContent delegate methods to update my table whe开发者_StackOverflow中文版n the searchText in my searchBar is changed.

Currently in my app, these delegate methods only get called when changes in coredata occur. Not when changing the resultsController predicate & executing the fetch.

If I simply call [self.tableView reloadData] on each textchange, there are no animations and it blocks the experience a bit.

Or should I do something completely different to update the tableView contents on each search text entry change?


I've had another look into Apple's documentation and it looks like the NSFetchedResultsControllerDelegate methods are not called when a predicate has changed:

An instance of NSFetchedResultsController uses methods in this protocol to notify its delegate that the controller’s fetch results have been changed due to an add, remove, move, or update operations.

So, to answer my own question: It's not possible to use NSFetchedResultsControllerDelegate to update the table after NSFetchedResultsController predicate changes.


Try to call [self.tableView reloadData] in

-(void)viewWillAppear:(BOOL)animated;
                 or
-(void)viewDidAppear:(BOOL)animated;


You could try using the UITableView method reloadSections:withRowAnimation: passing in an NSIndexSet with all of your sections. That way you should have the whole thing reload but still get the animation of your choice.

If you are adding and removing rows you will need to use insertSections:withRowAnimation: and removeSections:withRowAnimation:. If you're doing a number of changes at once you should also do them between calls to beginUpdates and endUpdates.


From NSFetchedResultsController Class Reference:

Modifying the Fetch Request

You cannot simply change the fetch request to modify the results. If you want to change the fetch request, you must:

If you are using a cache, delete it (using deleteCacheWithName:). Typically you should not use a cache if you are changing the fetch request.

Change the fetch request. Invoke performFetch:.

Since you have changed the predicate, you must call performFetch: once again and then the delegate will handle updating the table from there.

0

精彩评论

暂无评论...
验证码 换一张
取 消