Search code examples
iosswiftcore-datansfetchedresultscontrollernsfetchrequest

What is the best way to implement shorting(Multiple time on single data) and filter with core data with Swift 3?


[Core data provides NSFetchResultController to help with UITableView. I want to sort data with Duration, Depart, Arrival, Price, and Number of stops with ascending and descending orders. I con sort data by providing sort descriptor to fetch request.

I just want to know is it best way to create FetchResultController for each sorting?

enter image description here


Solution

  • Yes, you should create a new fetchedResultsController each time you need to sort. A fetchedResultsController tracks updates, deletes, inserts and moves, so if your data changes after you fetch it, the fetchedResultsController will tell you how to update your view. If you just do a fetch and then resort the results, the data can get out of date. And worse, if you access an object after it has been deleted your app will crash. A fetchedResultsController protects you from those issues.

    Even with large data sets I have not had any problem creating a new fetchedResultsController every time the user wishes to sort. If you find that you have performance problems you should deal with it then and not pre-optimize.