Realm add List<> object to already existing object

泄露秘密 提交于 2019-12-25 01:47:24

问题


This is how my Object classes looks like:

Workout.swift:

class Workout: Object {

    @objc dynamic var date: Date?
    // List of exercises (to-many relationship)
    var exercises = List<Exercise>()

}

Exercise.swift

class Exercise: Object {

    @objc dynamic var name: String?
    // List of sets (to-many relationship)
    var sets = List<Set>()
    var parentWorkout = LinkingObjects(fromType: Workout.self, property: "exercises")
}

Set.swift

class Set: Object {

    @objc dynamic var reps: Int = 0
    @objc dynamic var kg: Double = 0.0
    @objc dynamic var notes: String?
    // Define an inverse relationship to be able to access your parent workout for a particular set (if needed)
    var parentExercise = LinkingObjects(fromType: Exercise.self, property: "sets")

    convenience init(numReps: Int, weight: Double, aNote: String) {
       self.init()
       self.reps = numReps
       self.kg = weight
       self.notes = aNote
    }
}

And I have a workout that is already created:

[Workout {
    date = 2019-12-07 23:26:48 +0000;
    exercises = List<Exercise> <0x281ea5b00> (
        [0] Exercise {
            name = Barbell Biceps Curl;
            sets = List<Set> <0x281eb0090> (
                [0] Set {
                    reps = 10;
                    kg = 40;
                    notes = Light;
                },
                [1] Set {
                    reps = 10;
                    kg = 40;
                    notes = Light;
                },
                [2] Set {
                    reps = 12;
                    kg = 37.5;
                    notes = Hard;
                }
            );
        }
    );
}]

Now that I have a workout that has already been created, how can I add a new exercise to that exact workout, without creating a whole new workout, so I have two exercises registered in that specific workout?


回答1:


Since your users can only log in one workout per day, you can lookup that specific workout by date:

if let workout = realm.objects(Workout.self).filter("date = %@", date).first {
    // Workout found
    try! realm.write {
       workout.exercises.append(exerciseToAppend)
    }
} else {
   // Workout not found, handle it the way you want
}



回答2:


You want to:

  1. Fetch an existing Workout
  2. Create a new Exercise
  3. Append the Exercise to the Workout

I assume you know the primary key pkey of the Workout you want to fetch.

let myWorkout = realm.object(ofType: Workout.self, forPrimaryKey: pkey)! // 1: fetch an existing workout

let exercise = Exercise() // 2: create a new exercise with some sets
let set1 = Set(numReps: 1, weight: 1.0, aNote: "one")
let set2 = Set(numReps: 2, weight: 2.0, aNote: "two")
exercise.sets.append(objectsIn: [set1, set2])

try! realm.write {
    myWorkout.exercises.append(exercise) // 3: append
}


来源:https://stackoverflow.com/questions/59230780/realm-add-list-object-to-already-existing-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!