I want a thorough list regarding comparison between the two. Things I have known:
executeFetchRequest:
- Message sent to MOC
- Return an array of managed objects
- Goal: fetch objects from persistent store to MOC
- With table view: has nothing to do with table view
- Frequency: often used in a loop, so could be called many many times
performFetch:
- Message sent to FRC
- After calling it, use
fetchedObjectsto return an array of managed objects - With table view: FRC is specifically for keeping managed objects and table view rows in sync, and use
performFetchto initialize that process. - Frequency: often only once. Unless fetch request of FRC changes, no need to call
performFetcha second time
Please correct me if I am wrong and append the list. Thank you.
About
executeFetchRequest:Yes
Yes, but you can also change the type of results you want to retrieve. In
NSFetchRequestyou can set a different result type with:where
NSFetchRequestResultTypecan be of different types. Taken from Apple doc:Yes, creating a
NSFetchRequestand performing a request, it the same as creating a SELECT statement in SQL. If you also use aNSPredicateit’s the same as using SELECT-WHERE statement.Yes, but with retrieved data you can populate a table
It depends, on what you want to achieve. It could be within a loop or not. Executing the request within a loop could have impact on performance but I would not be worried on that. Under the hood Core Data maintains a sort of cache mechanism. Every time you perform a request, if data are not in the cache, Core Data executes a round trip on your store (e.g. sql file) and populate the cache with the objects it has retrieved. If you perform the same query, the round trip will not performed again due to the cache mechanism. Anyway, you could avoid to execute a request within the run loop, simply moving that request outside the loop.
About
performFetch:Yes
Yes, but you can also retrieve an object with
[_fetchedResultsController objectAtIndexPath:indexPath];if you are populating a specific cell within a table.Here I really suggest to read a nice tutorial on NSFetchedResultsController
Yes, a
NSFetchedResultsControllerworks in combination with aNSManagedObjectContextfor you. Furthermore, it enables lazy loading of data. Suppose you have 1000 elements you retrieve and you want to display them in aUITableView. Setting a request for aNSFetchRequestlike:and using it with an instance of a
NSFetchedResultsController, it allows to load 20 elements at first. Then when you scroll, other 20 elements are loaded, and so on. Without aNSFetchedResultsControlleryou must implement this behavior manually. Refer to the tutorial I provided for further info.It depends on what you want to achieve. Most of the time you could call it once.
Hope that helps.
Edit
You have to call
performFetchexplicitly. I like to create a property forNSFetchedResultsControllerin my header file (.h) likeand synthesize it in your implementation file (.m) like
Then always within the .m file override the getter to create an new instance of it:
Once done, within your class (for example in
viewDidLoadmethod) use it like