LinkingObjects subscribe in Swift?


#1

In this thread it seems possible with node.js to subscribe to a linkingObject:

Let’s translate this into swift, where I have a similar problem:

class Organization: Object {
    
    @objc dynamic var id: String = UUID().uuidString
    var jobs = List<Job>()
    override static func primaryKey() -> String? {
        return "id"
    }
}

class Job: Object {
    
    @objc dynamic var id: String = UUID().uuidString
    var organization = LinkingObjects(fromType: Organization.self, property: "jobs")
    override static func primaryKey() -> String? {
        return "id"
    }
}

class AViewController {
    
    func loadData() {
        
        let jobs = realm!.objects(Job.self)
        _ = jobs.subscribe()
        
        // jobs.first?.organization won't be loaded!
        
        // is something like this possible
        _ = jobs.first?.organization.subscribe()
        
    }
}

Both objects are partially-synced.

Currently, I see only four not so elegant approaches to get organization populated:

  1. Full-Sync Organization: That’s what I want to avoid.
  2. Inverse the object design so that I can subclass Organization in Job - but then I have the problem the other way round.
  3. Query and subscribe Organization in a way that all jobs are included, which is not possible in my case.
  4. Add organizationID to Job, which allows me to query & filter Organization, but has many other implications.

So, is it possible to subscribe to a LinkedObject like this:
_ = jobs.first?.organization.subscribe()

Or, is there something other I miss?


#2

Admittedly I haven’t tested it, but realm cocoa 3.15.0 added

Add an option to include the objects from RLMLinkingObjects/LinkingObjects properties in sync subscriptions, similarly to how RLMArray/List automatically pull in the contained objects


#3

Brilliant!

_ = jobs.subscribe(named: nil, limit:nil, update: nil, timeToLive: nil, includingLinkingObjects: [“organization”])

does the trick! :grinning: