How to get data in with notificationToken?

Hi,

I’m trying to use functionality similar to the Kotlin api:

    result.addChangeListener { results, changeSet ->
        if (changeSet == null) {
            // The first time async returns with an null changeSet.
        } else {
            // Called on every update.
        }
    }

In Swift apparently I’ve to use:

results.observe { [weak self] (changes: RealmCollectionChange) in ... }

But the change doesn’t have the objects itself. I only need the objects, not indices.
Do I have to store results in an instance variable and just access it in the closure? Or how to I get the up to date results?

Thanks.

The change contains a reference to the collection which contains the objects:

    collection.observe { (changes: RealmCollectionChange) in
                switch changes {
                case .initial(let results):
                    // your initial results type
                case .update(let results, let deletions, let insertions, let modifications):
                    XCTAssertEqual(results.count == 3)
                    // operate on results
                case .error:
                    XCTFail("Shouldn't happen")
                }
            }
1 Like