Query-based synchronization and retrieve single record by primary key


#1

I understand that with Query-based sync you do the following:

  1. build your query that returns a Results<[realm object]>
  2. subscribe to the query
  3. add a notification token to know when the query has been synced (i.e. data is available)

With the above in mind, does that mean when you choose to use a reference realm and query based sync, you can’t do the following and get back a single object:

let user = realm.object(ofType: User.self, forPrimaryKey: “C0725528-04C8-4D25-BB15-64FC11D0FAEE”)

Instead you would have to do something like:

let currentUserArr = realm.objects(DBUser.self).filter("id == %@", "C0725528-04C8-4D25-BB15-64FC11D0FAEE")
self.subscription = currentUserArr.subscribe(named: "current user")
            
            self.subscriptionToken = self.subscription.observe(\.state, options: .initial) { state in
                switch state {
                ...
                case .complete:
                    print("complete")
                    let user = currentUserArr[0]
                ...
                }
            }