All the solutions I have found online are for local Realms, not synced Realms (I am using Query based sync). How to do it right for a synced Realm?
I have a Shop
object and an Item
object. Shop
has many items. User can browse the items and should see which shop that item belongs to. Pretty simple and straight forward scenario.
In the Shop
class I have:
let items = List<Item>()
and in the Item
class I have
let shops = LinkingObjects(fromType: Shop.self, property: "items")
var shop: Shop? { return shops.first }
The Realm query is like this:
private var realm : Realm!
private var subscriptionToken : NotificationToken?
private var syncSubscription : SyncSubscription!
...
...
...
let items = realm.objects(Item.self)
syncSubscriptionItem = items.subscribe()
subscriptionTokenItem = syncSubscriptionItem.observe(\.state, options: .initial) { state in
if state == .complete {
let shopName = items[0].shop?.name // Shop is always nil
}
}
I can see shop only if the shop's owner has logged into the app, which means Realm has synced shop information to local Realm. But for users on other devices, shops never get synced. But how to sync shops for all other users via this type of back linking?
Screenshot is attached to clarify what I mean: