Hi,
I have an iOS App and a Core Data Model with three entities.
Entity A:
name: String
value: Int64
Entity B:
name: String
Entity C:
relationship1: A
relationship2: B
value: Int64
Now I want to fetch all Entities of type "C" wich have a relationship to "A" for a given name of A
Instead of a list of those Cs I'd like to return a number: The sum of (C.value * C.relationship1.value) for all fetched Cs. (line 07)
Because I have no idea how to do this using a FetchRequest, I thought I could use a computed property for C ("product") - which works fine - but I can't access it during the FetchRequest. I think it crashes in the line with the "right here" comment.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'keypath sum not found in entity <NSSQLEntity C id=2>'
import Foundation
import CoreData
@objc(C)
public class C: NSManagedObject {
@objc dynamic var product : Int64 {
get {
if let test = self.relationship1.value {
return test * self.value
} else {
return 0
}
}
}
}
let fetchRequest = NSFetchRequest<NSDictionary>(entityName: "C")
fetchRequest.resultType = .dictionaryResultType
let sumExpressionDesc = NSExpressionDescription()
sumExpressionDesc.name = "returnValue"
let predicate = NSPredicate(format: "C.relationship1.name == %@", name)
let expression = NSExpression(forKeyPath: #keyPath(C.product)) //right here
sumExpressionDesc.expression = NSExpression(forFunction: "sum:", arguments: [expression])
sumExpressionDesc.expressionResultType = .integer32AttributeType
fetchRequest.predicate = predicate
fetchRequest.propertiesToFetch = [sumExpressionDesc]
do {
let results = try managedObjectContext.fetch(fetchRequest)
let resultDict = results.first!
if let value = resultDict["returnValue"], let count = value as? Int {
return count
}
} catch {
print(error)
}
return -1
I'm glad about any refinements as well as for a solution.
Thanks in advance!