6

I am targeting iOS 10 and can't decide which method should I choose and why? Since both of the below code returns me array of objects.

    CoreDataStack.shared.viewContext.perform {
        do {
            let allObjects = try CoreDataStack.shared.viewContext.fetch(fetchRequest)
            print(allObjects)
        } catch {
            print("Error: \(error)")
        }
    }
    CoreDataStack.shared.viewContext.perform {
        do {
            let allRules = try fetchRequest.execute()
            print(allRules)
        } catch {
            print("Error: \(error)")
        }
    }
Meanteacher
  • 2,031
  • 3
  • 17
  • 48
  • See (duplicate?) https://stackoverflow.com/questions/38387125/how-do-i-make-a-fetch-request-using-nsmanagedobjects-new-fetchrequest-function. – Martin R Jul 08 '17 at 11:23
  • I saw but as I said, this post also says there are two ways of doing it. I have nor problem getting the objects as my code shows. I wonder the difference or drawbacks if any. – Meanteacher Jul 08 '17 at 12:27
  • As I understand it from the other answer, the only difference is that `fetchRequest.execute()` *must* be called inside a `perform` block. – Martin R Jul 08 '17 at 12:33
  • Since I am calling both of them inside the block, are they basically the same? I guess using second one is better since it's syntax looks cleaner. – Meanteacher Jul 08 '17 at 13:05
  • @MartinR _Both_ methods must always be called on the proper managed object context. The docs for `NSFetchResult execute` are just incomprehensible - while the docs for `NSManagedObjectContext fetch` are pretty clear. – CouchDeveloper May 28 '19 at 09:31

0 Answers0