Does CoreStore implements somehow havingPredicate for groupBy ? #330

Open
opened 2025-12-29 15:29:16 +01:00 by adam · 1 comment
Owner

Originally created by @vitt0re on GitHub (Jun 2, 2020).

Hello
I need to migrate the code like one below to CoreStore

       let dupeFetch: NSFetchRequest<NSFetchRequestResult> = StopEntity.fetchRequest()

        let nameExpr = NSExpression(forKeyPath: "name")
        let countExpr = NSExpressionDescription()
        let countVariableExpr = NSExpression(forVariable: "count")

        countExpr.name = "count"
        countExpr.expression = NSExpression(forFunction: "count:", arguments: [nameExpr])
        countExpr.expressionResultType = .integer64AttributeType

        dupeFetch.resultType = .dictionaryResultType
        dupeFetch.propertiesToGroupBy = ["latitude", "longitude"]
        dupeFetch.propertiesToFetch = ["latitude", "longitude", countExpr]
        dupeFetch.returnsObjectsAsFaults = false
        dupeFetch.havingPredicate = NSPredicate(format: "%@ > 1", countVariableExpr)

        var dupes = [Any]()
        do {
            dupes = try dataManager.context.fetch(dupeFetch)
        } catch {
            print(error.localizedDescription)
        }

it uses propertiesToGroupBy and havingPredicate but I can not find any mechanism in CoreStore to use for it.
Does exist any way to use it ?

Originally created by @vitt0re on GitHub (Jun 2, 2020). Hello I need to migrate the code like one below to CoreStore ``` let dupeFetch: NSFetchRequest<NSFetchRequestResult> = StopEntity.fetchRequest() let nameExpr = NSExpression(forKeyPath: "name") let countExpr = NSExpressionDescription() let countVariableExpr = NSExpression(forVariable: "count") countExpr.name = "count" countExpr.expression = NSExpression(forFunction: "count:", arguments: [nameExpr]) countExpr.expressionResultType = .integer64AttributeType dupeFetch.resultType = .dictionaryResultType dupeFetch.propertiesToGroupBy = ["latitude", "longitude"] dupeFetch.propertiesToFetch = ["latitude", "longitude", countExpr] dupeFetch.returnsObjectsAsFaults = false dupeFetch.havingPredicate = NSPredicate(format: "%@ > 1", countVariableExpr) var dupes = [Any]() do { dupes = try dataManager.context.fetch(dupeFetch) } catch { print(error.localizedDescription) } ``` it uses **propertiesToGroupBy** and **havingPredicate** but I can not find any mechanism in CoreStore to use for it. Does exist any way to use it ?
adam added the enhancement label 2025-12-29 15:29:16 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jun 3, 2020):

There's no default utility for it yet, but you can also use GroupBy in tandem with Tweak to achieve the same effect:

let countKey = "numberOfDuplicates"
let latitudeKey = "lat"
let longitudeKey = "long"
let dupes: NSDictionary = dataStack.fetchAll(
    From<StopEntity>()
        .select(
            NSDictionary.self, 
            .count(\.name, as: countKey), 
            .attribute(\.latitude, as: latitudeKey),
            .attribute(\.longitude, as: longitudeKey)
         )
        .groupBy(latitudeKey, longitudeKey)
        .tweak(
            { $0.havingPredicate = Where<StopEntity>("%K > %@", countKey, 1).predicate }
        )
)

Thanks for raising this. I'll consider adding this extension to GroupBy clauses.

@JohnEstropia commented on GitHub (Jun 3, 2020): There's no default utility for it yet, but you can also use `GroupBy` in tandem with `Tweak` to achieve the same effect: ```swift let countKey = "numberOfDuplicates" let latitudeKey = "lat" let longitudeKey = "long" let dupes: NSDictionary = dataStack.fetchAll( From<StopEntity>() .select( NSDictionary.self, .count(\.name, as: countKey), .attribute(\.latitude, as: latitudeKey), .attribute(\.longitude, as: longitudeKey) ) .groupBy(latitudeKey, longitudeKey) .tweak( { $0.havingPredicate = Where<StopEntity>("%K > %@", countKey, 1).predicate } ) ) ``` Thanks for raising this. I'll consider adding this extension to `GroupBy` clauses.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#330