I have a view that contains a tableView. I'm using NSFetchedResultsController
to display my results from Core Data.
I'd like to add a UISearchController (not a UISearchDisplayController
as this one is deprecated in iOS 8) but I don't know how to link them.
Should I have only one NSFetchedResultsController
or two?
I guess there is a way to fetch all the data with the NSFetchedResultsController
and then just sort them according to the UISearchController
am I wrong?
Thanks for the help you can give me on this.
A few thoughts...
When you create the resultsController
for the searchController
, you can pass to it the data that you wish to search. If you are using an NSFetchedResultsController
in your main table, you could pass the fetchedObjects
array. Then in response to changes to the search text, you filter the array (you could use a predicate and filteredArrayUsingPredicate
to create a separate array with the search results, or you could iterate through the array to build it). The disadvantages of this route are that (unless you implement it manually) the search results will not be broken into separate sections, and the search results will not update automatically if the underlying data changes (eg. on a background thread).
I guess you could have a second NSFetchedResultsController
: that would facilitate using sections, and could potentially permit the results to be updated automatically (using the delegate methods) if your data is being updated in the background, for example. But I would be nervous of the complexity that it introduces.
Another option, if you opt to apply the search in situ (i.e. specify resultsController
= nil), would be to use the search criteria to update the NSFetchedResultsController
itself (i.e. amending the underlying predicate and reperforming the fetch). That way your search table looks and feels exactly like the main table (including sections, if you use that) but the rows displayed obviously reduce as the search criteria become finer. This option needs care to ensure that the FRC is properly rebuilt, and might be unacceptable performance-wise, if you have a large dataset.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With