あなたが望むものを達成するために(私が思うに)、2つの別々のフェッチに頼らなければなりませんでした。country
最初のフェッチでは、との個別の組み合わせごとに 1 つのオブジェクトの objectID を取得しcity
ます。2 番目のフェッチは、IN
述語を使用して、これらのオブジェクトだけにフィルター処理されます。と を使用NSExpression
しpropertiesToGroupBy
て、各 のカウントを取得しますcountry
。
// Step 1, get the object IDs for one object for each distinct country and city
var objIDExp = NSExpression(expressionType: NSExpressionType.EvaluatedObjectExpressionType)
var objIDED = NSExpressionDescription()
objIDED.expression = objIDExp
objIDED.expressionResultType = .ObjectIDAttributeType
objIDED.name = "objID"
var fetch = NSFetchRequest(entityName: "Location")
fetch.propertiesToFetch = [objIDED]
fetch.propertiesToGroupBy = ["country", "city"]
fetch.resultType = .DictionaryResultType
let results = self.managedObjectContext!.executeFetchRequest(fetch, error: nil)
// extract the objectIDs into an array...
let objIDArray = (results! as NSArray).valueForKey("objID") as! [NSManagedObjectID];
// Step 2, count using GROUP BY
var countExp = NSExpression(format: "count:(SELF)")
var countED = NSExpressionDescription()
countED.expression = countExp
countED.expressionResultType = .ObjectIDAttributeType
countED.name = "count"
var newFetch = NSFetchRequest(entityName: "Location")
newFetch.predicate = NSPredicate(format: "SELF IN %@", objIDArray)
newFetch.propertiesToFetch = ["country", countED]
newFetch.propertiesToGroupBy = ["country"]
newFetch.resultType = .DictionaryResultType
let newResults = self.managedObjectContext!.executeFetchRequest(newFetch, error: nil)
println("\(newResults!)")
これは非効率的です: 個別の国や都市が多数ある場合、IN 述語は速度を低下させます。すべてをフェッチしてカウントする方が効率的であることに気付くかもしれません。