Getting a count of records in a core data entity
func getRecordsCount() {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: _entityName)
do {
let count = try context.count(for: fetchRequest)
print(count)
} catch {
print(error.localizedDescription)
}
}
I believe the best way to accomplish this is through NSManagedObjectContext's countForFetchRequest:error:
It works just like a regular fetch request, except that it only returns the count and presumably could therefore be more optimized.