I have a stop that has many destinations. I managed to do this using the linkingObjects function, but it has been deprecated since the last update, and I have to use the LinkingObjects object.
Stop
class Stop: Object { dynamic var name:String = "" var directions = List<Direction>() override static func primaryKey() -> String? { return "name" } }
Old direction
class Direction: Object { dynamic var tag:String = "" var stop:Stop? { return linkingObjects(Stop.self, forProperty: "directions").first } }
When I apply my previous approach to a new object, I always get nil
New null direction returned by LinkingObjects
class Direction: Object { dynamic var tag:String = "" let stop = LinkingObjects(fromType: Stop.self, property: "directions").first
But here I get an array with one element. Therefore, it works as it should.
New null direction returned by LinkingObjects
class Direction: Object { dynamic var tag:String = "" let stops = LinkingObjects(fromType: Stop.self, property: "directions") }
Question
Is there any other way to use "LinkingObjects" and not this last example, because in each case "direction.stop.first? .Name" instead of "direction.stop? .Stop"?
Of course, I could use the 'direction' function, which would always select the first element in the 'stop', but maybe I don't need to.
UPDATE
Meanwhile, I use this. This is an ideal solution, but it works.
private let stops:LinkingObjects<Stop> = LinkingObjects(fromType: Stop.self, property: "directions") var stop:Stop? { return self.stops.first }