My question is, how would one derive a total sum for a single property irrespective of any relationship for every saved instance of a Core Data entity?
This CoreData question is different from the others that I have seen because the others speak on deriving a property from a relationship defined in the CoreData GUI. There is no relationship here. I would just like to get the total for every saved instance on a single CoreData entity*.
Hopefully the video will help.
Big Picture
Goal: Mapping over all a single CoreData Property for every instance of the entity.
I have been doing a lot of research on this lately and am currently waiting on a response from the Apple technical team. First let me say that I have watch the WWDC Vid on Core Data and it is very helpful.
But they speak about how to derive properties from multiple relationships. This is a single relationship that I would like to map over.
And all other resources that I've checked on the topic, (a good deal can be found here). Thanks to @lorem ipsum for the resources he shared.
But these resources again speak about how to aggregate properties from a different relationship.
So just to be clear, how would one derive a total sum for a single property irrespective of any relationship for every saved instance of a Core Data entity.
I would be happy if someone can even suggest how I would get an array or dictionary of all the saved instances.
Adding more detail
This is the class I am using (simplified)
import Foundation
import CoreData
@objc(Ticket)
public class Ticket: NSManagedObject { }
extension Ticket {
@NSManaged var company: String
@NSManaged var hoursWorked: Int64
}
And here is a mock func
to try and convey the idea of what I am trying to do:
This is a completely bogus func
just hoping to add clarity to what I'm trying to do.
func getAllHoursForCompany(_ company: String, using context: NSManagedObjectContext) -> Int {
var allHours = [Int]()
let workingCompany: Ticket(context: context) where Ticket.company == company
for hours in workingCompany {
allHours.append(hours)
}
return allHours.reduce(0) { $0 + $1 }
}