0

[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

Harshal Valanda
  • 5,331
  • 26
  • 63
Rohit Sisodia
  • 895
  • 12
  • 13
  • why you not read : https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/FetchingObjects.html and use **NSPredicate** – Nitin Gohel Jun 05 '17 at 07:19
  • Yes i can fetch data with predicate but i want to sort data(multiple times) after fetching, So should i use FetchResultController and change request for each sorting or Need to create array of fetched data for sorting. Which one is best? – Rohit Sisodia Jun 05 '17 at 07:47
  • why after fatching you shoud fatch with query with sort:) – Nitin Gohel Jun 05 '17 at 08:11
  • I have flights data with depart, Arrival, Number of stops, Price, and journey duration. I need to fetch data from core data. Now user can sort data (Ascending or descending) according price, depart and so on. – Rohit Sisodia Jun 05 '17 at 09:25

1 Answers1

1

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.

Jon Rose
  • 8,373
  • 1
  • 30
  • 36