简体   繁体   中英

Realm Swift - save a reference to another object

I thought this would be pretty straightforward after reading here and here but I'm a bit stuck.

I have a 'favouriteWorkout' object that looks like this :

class FavouriteObject: Object {
    @objc dynamic var favouriteWorkoutName = ""
    @objc dynamic var workoutReference = WorkoutSessionObject()

    override class func primaryKey() -> String? {
        return "favouriteWorkoutName"
    }
}

What I'm trying to do here is reference a WorkoutSessionObject in Realm that links from a WorkoutName when a workout is saved as a favourite.

My WorkoutSessionObject has a primary key of workoutID which is a UUID string. It looks like this :

class WorkoutSessionObject: Object {
    @objc dynamic var workoutID = UUID().uuidString
    @objc dynamic var workoutType = ""
    let exercises = List<WorkoutExercise>()
    @objc dynamic var totalExerciseCount = 0
    @objc dynamic var rounds = 0
    @objc dynamic var favourite : Bool = false

    override class func primaryKey() -> String? {
        return "workoutID"
    }
}

I've then tried to save using this :

let favouriteWorkout = FavouriteObject()
favouriteWorkout.favouriteWorkoutName = favouriteName
favouriteWorkout.workoutReference = (realm.object(ofType: WorkoutSessionObject.self, forPrimaryKey: self.workoutID))!

do {
    try realm.write {
        realm.add(favouriteWorkout)
    }
} catch {
    print ("Error adding favourite")
}

but i get a crash when I run of :

'RLMException', reason: 'The FavouriteObject.workoutReference property must be marked as being optional.

However, when I then try to make it optional (by adding ?) it says

"Cannot use optional chaining on non-optional value of type 'WorkoutSessionObject"!

Summary

I want to save a reference of the workoutID of a WorkoutSessionObject in my FavouriteObject which is an actual link to the WorkoutSessionObject (so the properties can be accessed from favourites)

Update

using the answers below I've now sorted the problem of the workout reference. This is now showing in Realm as the proper format () under "workoutReference". However, I'm now getting "nil" in "workoutReference" when trying to save. I know the workoutID is coming through correctly as I am printing it in the console.

In property-cheatsheet you can see that a non-optional Object -property is not allowed, so you have to change it like the following:

class FavouriteObject: Object {
    @objc dynamic var favouriteWorkoutName = ""
    // here you have to make the property optional
    @objc dynamic var workoutReference: WorkoutSessionObject?

    override class func primaryKey() -> String? {
        return "favouriteWorkoutName"
    }
}

You need to change the declaration of workoutReference . First of all, you need to make it Optional by writing ? after the type . Secondly, you shouldn't assign a default value to it, it needs to be Optional for a reason. The linked docs clearly state that

to-one relationships must be optional

, and workoutReference is clearly a to-one relationship.

class FavouriteObject: Object {
    @objc dynamic var favouriteWorkoutName = ""
    @objc dynamic var workoutReference:WorkoutSessionObject?

    override class func primaryKey() -> String? {
        return "favouriteWorkoutName"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM