Cocoa Core Data efficient way to count entities

后端 未结 9 1767
滥情空心
滥情空心 2020-11-30 16:45

I read much about Core Data.. but what is an efficient way to make a count over an Entity-Type (like SQL can do with SELECT count(1) ...). Now I just solved this task with s

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 17:41

    In Swift 3

      static func getProductCount() -> Int {
        let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
        let fetchRequest = NSFetchRequest(entityName: "Product")
        let count = try! moc.count(for: fetchRequest)
        return count
    }
    

提交回复
热议问题