I have a core data model that's hooked up to a Storyboard GUI.(Xcode 9 and SWIFT 4) I now want to add a property which is derived from two others. I have an Entity called Workout and two of it's properties "rpe" and "seconds" are combined to create a new property "rpeTSS". I have declared "repTSS" as an attribute in my core data model. I've set it to transient.
I have tried using the "make NSManagedObject subclass..." option under the Editor menu but this seemed to produce files with errors. I tried subclassing the generated Workout class but couldn't figure out how to bind that in to the GUI.
I then made an extension to Workout:
extension Workout{
public override func value(forKey key: String) -> Any? {
if key == "rpeTSS"{
return (100/49)*rpe*rpe*Double(seconds)/3600
}else{return super.value(forKey: key)}
}
}
I'll be honest this doesn't 'feel' right. Anyway it (kind of) works. I do now get the correct value for rpeTSS and it shows up in my gui. However if I make changes to rpe and/or seconds the GUI doesn't update.
I'm guessing the method above is a bit of a hack and thus circumvents various messaging that gets the GUI to update.
What is the correct way to add derived properties within core data? I'm trying to achieve this in a way that means if I add anything to my data model I don't have to go re-writing of the code for the derived properties.