【问题标题】:Realm add List<> object to already existing object领域将 List<> 对象添加到已经存在的对象
【发布时间】:2019-12-07 22:27:34
【问题描述】:

这就是我的Object 类的样子:

锻炼.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")
}

设置.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
    }
}

我已经创建了一个锻炼:

[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;
                }
            );
        }
    );
}]

现在我已经创建了一个锻炼,我如何在不创建全新锻炼的情况下向该锻炼添加新锻炼,这样我在该特定锻炼中注册了两个锻炼?

【问题讨论】:

  • 听起来你想使用主键stackoverflow.com/questions/26152254/…
  • @OkhanOkbay 所以每次锻炼都应该有一个主键?
  • 是的。 Realm 会自动更新锻炼而不是创建新的锻炼
  • @OkhanOkbay 我是否自己为每次锻炼提供密钥?以及如何避免使用两个相同的主键创建锻炼?
  • 只需保留一个 ID 属性,将锻炼计数分配给您将记录到 Realm 的下一个锻炼。请阅读有关它的 Realm 文档

标签: ios swift xcode realm realm-list


【解决方案1】:

你想:

  1. 获取现有的Workout
  2. 创建一个新的Exercise
  3. Exercise 附加到Workout

我假设您知道要获取的 Workout 的主键 pkey

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
}

【讨论】:

  • 很好的答案,一般来说,对于 Realm 对象来说,使用Primary Keys 来帮助区分一个对象和另一个对象通常是一种很好的做法。虽然您可以使用“日期”属性,但同一天有两次锻炼?澄清一点可能是显而易见的。在let exercise = Exercise() 之后,您还可以创建一个集合并将其添加到集合属性中,然后将练习附加到锻炼中 - 一个空的练习将不包含任何数据,然后必须再次加载以使用集合更新它.
【解决方案2】:

由于您的用户每天只能登录一次锻炼,您可以按日期查找特定锻炼:

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
}

【讨论】:

  • 很好的答案,假设每天只有一次锻炼,如果你以后每天想要更多怎么办?没有一种简单的方法可以将两者区分开来。主键是去这里的方式。
  • @Jay 每天只有一次锻炼,因为锻炼是许多锻炼的“集合”。所以这个应用程序每天不会有超过一次的锻炼:)
  • @Camile 明白了。在这种情况下,这个答案会给你你想要的结果。如果您想对此进行扩展,请参阅我对您的其他问题 Check if Realm data has been added on a specific date 的回答,了解其他一些选项。
猜你喜欢
  • 2012-09-12
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多