My car object has four passenger objects. The car needs to know where the passengers are sitting, but the reverse relations wouldn’t make sense, i.e. frontPassengerSeatOccupant would be logical, but carWhosFrontPassengerSeatImSittingIn seems pointless (then I’d need three others for the other three seats), and I’d rather just have one carImSittingIn.
CoreData is recommending that I have inverse relationships for everything except in “advanced” cases, so I’m trying to abide by that. So I’m trying to do an ordered occupants relation, where seat 0 is driver, 1 is front passenger, 2 is rear driver, 3 is rear passenger. The problem occurs when I don’t have anybody in seat 1, but i have somebody in seat 2 or 3, because I can’t put a placeholder in the array.
What would be a clean way to implement this ordered, possibly empty seat problem while maintaining inverse relationships, and not having all these extra pointless properties?
Two ways you could do this: provide an object that represents an unoccupied seat, or create an intermediate
Seatobject that contains the participants.Core Data models ordered relationships as an
NSOrderedSet.NSOrderedSetstores objects contiguously (i.e. it won’t contain anynilvalues) You could create a dummy object that represents an unoccupied seat. You’d need to track the “null occupant” object, and I imagine you’d end up with a lot of complicatedif ([occupant isNullOccupant])logic to deal with this special case.The other solution, which I think is cleaner, is to have three entities:
Car,Seat, andOccupant. A car would have a to-many relationship withSeat, and aSeatwould have a to-one relationship withOccupant. Your model would look like this: