Core data: executeFetchRequest vs performFetch

前端 未结 2 862
挽巷
挽巷 2021-02-08 00:40

I want a thorough list regarding comparison between the two. Things I have known:

executeFetchRequest:

  • Message sent to MOC
  • Return an
2条回答
  •  粉色の甜心
    2021-02-08 01:21

    You are comparing the wrong elements. NSFetchedResultsController uses the NSManagedObjectContext to perform the fetch, and under proper configuration, monitors the changes in the managed object context to verify the status of the fetch properties it is monitoring, but the actual fetches are done by the context. On both cases, NSManagedObjectContext does the fetch. The difference being that, using the NSManagedObjectContext directly, you get an NSArray type of object (the actual runtime class is different than an array you get using [NSArray array]), while NSFetchedResultsController has a different purpose (have a collection of results and monitor changes to the records and entity on its fetch request). In other words, NSFetchedResultsController works using the context, but it works different than just a simple collection of objects.

    One observation: you shouldn't be using executeFetchRequest inside a loop, especially calling it "many many times". Each fetch has its performance cost. You can call executeFetchRequest once, and do a loop to check the result.

提交回复
热议问题