Skip to content

An AsyncSequence implementation for KVO observation

License

Notifications You must be signed in to change notification settings

simba909/KVOSequence

Repository files navigation

KVOSequence

Test

A small library enabling seamless AsyncSequence support for KVO observation, similar to NSObject.KeyValueObservingPublisher in Combine.

Usage

You can retrieve an AsyncSequence using both KeyPath and String-based APIs. For KeyPath:

class Person: NSObject {
    @objc dynamic var name: String?

    init(name: String? = nil) {
        self.name = name
    }
}

let person = Person()
Task {
    // The type of each change is inferred from the keypath
    for change in person.sequence(for: \.name, options: [.old, .new]) {
        print(change.newValue)
    }
}

// Prints "John"
person.name = "John"

and for String:

let defaults = UserDefaults.standard
Task {
    let sequence = defaults.sequence(
        // Since the type cannot be inferred from String-based APIs,
        // we need to manually specify the expected type here.
        of: [String: Any].self,
        forKeyPath: "user",
        options: [.new]
    )

    for change in sequence {
        print(change.newValue)
    }
}

// Prints ["name": "Alice", "age": 23]
defaults.set(["name": "Alice", "age": 23] as [String: Any], forKey: "user")

About

An AsyncSequence implementation for KVO observation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages