6

MY DESIGN

Im making a Fitness app to learn Core data, and I have found that I need to let the user store every performed workout as a WorkoutLog item, and in there, there should be a series of ExerciseLogs which represents performances of that exercise (it contains each lift and also a reference to the actual exercise design).

Problem is that after a while i realize that i need to have these ordered, so that the next time i want to show the user their workout, the order that the exercisese were performed should be the same.

So I checked "ordered" in the top right of the image there, and now my code is in dire need of an update. I have tried to read as much as I could about working with NSOrderedSet and how to fetch them from core data and then manipulate them, but I havent really found much of use to me. (I have no experice in objective-c)

For example my code that used to be:

static func deleteWorkoutLog(_ workoutLogToDelete: WorkoutLog) {

    guard let exerciseLogsToDelete = workoutLogToDelete.loggedExercises as? Set<ExerciseLog> else {
        print("error unwrapping logged exercises in deleteWorkoutLog")
        return
    }

I get the error: .../DatabaseFacade.swift:84:77: Cast from 'NSOrderedSet?' to unrelated type 'Set' always fails

So what ive learned about sets and core data no longer seems applicable.

Im far from an expert in programming, but im very eager to learn how to get access to the loggedExercises instances.

TLDR; Is there a way to cast NSOrderedSet to something I can work with? How do we usually work with NSManagedSets from core data? Do we cast them to Arrays or MutableSets? I would very much appreciate an example or two on how to get started with retrieving and using these ordered sets!

Thanks

Lord Fresh
  • 636
  • 8
  • 15
  • Are the exercises arbitrarily ordered by the user, or are they a set order based on date? – Jon Rose Aug 17 '17 at 06:26
  • @JonRose I wanted the user to be able to arbitrarily arrange the exercises. I could possibly have given them an index, but I figured using an ordered set would be more appropriate! – Lord Fresh Aug 17 '17 at 13:21

1 Answers1

5

For anyone else wondering how to get started with orderedSets in core data:

After setting my the WorkoutLog.loggedExercises "to-many" relationship to be ordered, I managed to access them through the mutableOrderedSetValue function like this:

static func deleteWorkoutLog(_ workoutLogToDelete: WorkoutLog) {

  let orderedExerciseLogs: NSMutableOrderedSet = workoutLogToDelete.mutableOrderedSetValue(forKey: "loggedExercises")

  let exerciseLogsToDelete = orderedExerciseLogs.array

  for exerciseLog in exerciseLogsToDelete {

    guard let exerciseLog = exerciseLog as? ExerciseLog else {
      return
    }

Works great so far.

And to rearrange the NSOrderedSet I ended up doing something like this:

    // Swap the order of the orderedSet
    if let orderedExerciseLogs: NSOrderedSet = dataSourceWorkoutLog.loggedExercises {

        var exerciseLogsAsArray = orderedExerciseLogs.array as! [ExerciseLog]

        let temp = exerciseLogsAsArray[indexA]
        exerciseLogsAsArray[indexA] = exerciseLogsAsArray[indexB]
        exerciseLogsAsArray[indexB] = temp

        let exerciseLogsAsOrderedeSet = NSOrderedSet(array: exerciseLogsAsArray)
        dataSourceWorkoutLog.loggedExercises = exerciseLogsAsOrderedeSet

    }
Lord Fresh
  • 636
  • 8
  • 15