0

How can one get the most frequent value used for an attribute in Core Data?

Let's say that our Entity table is:

--------------------------
|   Name   |   Company   |
--------------------------
|   John   |  Boring Co. |
|   Jane   |  Boring Co. |
|  Robert  |  Boring Co. |
|   Mary   | MoneyPal Co |
|   Jenn   | MoneyPal Co |
--------------------------

My objective is to get the most repeated value as "Company" attribute, i.e. Boring Co. in this example, without fetching every entity and looping through each item if possible.

monroo
  • 429
  • 3
  • 10
  • 1
    Do a fetch request for "Company", this results in an Array, then you can use a (counted) set to find the highest occurrence. See also here: https://stackoverflow.com/questions/30545518/how-to-count-occurrences-of-an-element-in-a-swift-array – koen Oct 25 '18 at 15:04
  • Maybe [this article](https://www.cocoanetics.com/2017/04/group-by-count-and-sum-in-coredata/) can get you started. – Joakim Danielson Oct 25 '18 at 15:04

1 Answers1

0

I'd like share what I've ended up using, in case anyone lands on this page looking for the answer to a similar question. My original intention was to use a Core Data/SQL fetching technique or a special predicate of some sorts to achieve this but I think there aren't any. So I implemented the following:

class CoreData_User:NSManagedObject{
    static func getMostFrequentCompany(context: NSManagedObjectContext ) -> String?{
        // We get a list of distinc Company entries
        guard let companies:[String] = CoreData_User.getDistinctCompanies(context: context) else { return nil }

        var maxRepeatedCompany:String = ""
        var maxRepeatCount:Int = 0

        // Then we loop through them to find which one is most oftenly used
        for currentCompany in companies{
            let currentCompanyRepeatCount:Int = CoreData_User.count(withCompany: currentCompany, context: context)
            if currentCompanyRepeatCount > maxRepeatCount{
                maxRepeatedCompany = currentCompany
                maxRepeatCount = currentCompanyRepeatCount
            }
        }
        if maxRepeatCount < 1 || maxRepeatedCompany.isEmpty {
            return nil
        } else {
            return maxRepeatedCompany
        }
    }

    static func getDistinctCompanies(context: NSManagedObjectContext ) -> [String]?{
        let request:NSFetchRequest<NSFetchRequestResult> = CoreData_User.fetchRequest()

        let companyAttributeName:String = "Company"

        request.resultType = .dictionaryResultType
        request.propertiesToFetch = [companyAttributeName]
        request.returnsDistinctResults = true
        var results:[String] = [String]()

        context.performAndWait{
            do{
                guard let fetchResults = try context.fetch(request) as? [NSDictionary] else { return }

                for item in fetchResults{
                    if let company = (item as AnyObject).value(forKey: companyAttributeName) as? String{
                        results.append(company)
                    }
                }
            } catch {
                print("Failed to get company names")
            }
        }

        if results.count > 0 {
            return results
        } else {
            return nil
        }
    }
    static func count(withCompany company: String, context: NSManagedObjectContext) -> Int{
        let request = NSFetchRequest<T>(entityName: type.entity().name!)
        request.includesSubentities = false
        request.predicate = NSPredicate(format: "Company ==[c] %@", company)

        var fetchResult:Int?
        context.performAndWait{
            fetchResult = try? context.count(for: request)
        }

        if let count = fetchResult {
            return count
        } else {
            return 0
        }
    }
}

Any suggestion on ways to improve and optimize is welcomed.

monroo
  • 429
  • 3
  • 10