I'm writing an iOS app in Swift that calculates a '1 rep maximum' weight using two inputs, weightLifted and repetitions. One of several formulas is used to perform the calculation with these two inputs to arrive at the answer which is the 1 rep max. I'm only storing the input values, not the 1 rep max value. I'm also storing the weightUnit (kg or lb) of the weightLifted.

extension LiftEvent {
    @NSManaged var date: Date
    @NSManaged var repetitions: NSNumber
    @NSManaged var uid: NSNumber?
    @NSManaged var weightLifted: NSNumber
    @NSManaged var weightUnit: String
    @NSManaged var formula: Formula
    @NSManaged var lift: Lift

I chose this approach because the computational cost is negligible and it makes weight unit conversion simple.

However, I've now discovered an unfortunate side effect of using an NSFetchedResultsController and have another choice to make. Each calculation result can be saved as a LiftEvent (the NSManagedObject seen above) but I can't sort by the 1 rep max result if I'm using an NSFetchResultsController because it's a derived property that is only created at runtime. It appears my choices are:

  1. persist the 1 rep max value as another property of the LiftEvent so it can be included as a sort descriptor in the fetch request used by the NSFetchedResultsController
  2. keep my data model the way it is, dump the NSFetchedResultsController, and instead create an array to hold my fetch request results, allowing me to convert and sort as needed, then make it my data source.

Option 1 is probably easier, but I never like to use that as a reason to do something. Unfortunately, because of the limitations of working with the fetched results once fetched make this about the only option I can think of that would allow me to keep the NSFetchedResultsController

What other things should I consider when making this decision?

  • 1
    This answer may help you stackoverflow.com/questions/2745999/… – Samuel Jul 12 '17 at 21:22
  • Thank you, Samuel. After a brief look at it, I think you might be right. – Jim Jul 12 '17 at 22:56
  • Thanks for the tip, but unless something has changed it doesn't look like a transient variable will solve my problem (stackoverflow.com/questions/38381794/…). I created a transient variable to return the calculated value and that worked just fine, but what I want to do is sort by that calculated value and it appears that it's just not possible if I'm using an NSFetchedResultsController and I'll need to make a choice from the options I've described. Any thoughts on that? – Jim Jul 16 '17 at 18:43
  • I would probably go with option #1 (add a persistent property) and use key-value observation to update that property automatically if the value of weightLifted or repetitions changes. – Martin R Oct 28 '18 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.