Dealing with change is at the core of Combine. Publishers let you subscribe to them to handle asynchronous events. In earlier chapters, you learned about assign(to:on:) which enables you to update the value of an object‘s property every time a publisher emits a new value.
But, what about a mechanism to observe changes to single variables?
Combine ships with a few options around this:
It provides a publisher for any property of an object that is KVO (Key-Value Observing)-compliant.
The ObservableObject protocol handles cases where multiple variables could change.
Introducing publisher(for:options:)
KVO has always been an essential component of Objective-C. A large number of properties from Foundation, UIKit and AppKit classes are KVO-compliant. Therefore, you can observe their changes using the KVO machinery.
It’s easy to observe KVO-compliant properties. Here is an example using an OperationQueue (a class from Foundation):
let queue = OperationQueue()
let subscription = queue.publisher(for: \.operationCount)
.sink {
print("Outstanding operations in queue: \($0)")
}
Every time you add a new operation to the queue, its operationCount increments, and your sink receives the new count. When the queue has consumed an operation, the count decrements and again, your sink receives the updated count.
There are many other framework classes exposing KVO-compliant properties. Just use publisher(for:) with a key path to a KVO-compliant property, and voilà! You get a publisher capable of emitting value changes. You’ll learn more about this and available options later in this chapter.
Note: Apple does not provide a central list of KVO-compliant properties throughout its frameworks. The documentation for each class usually indicates which properties are KVO-compliant. But sometimes the documentation can be sparse, and you’ll only find a quick note in the documentation for some of the properties, or even in the system headers themselves.
Preparing and Subscribing to Your Own KVO-compliant Properties
You can also use Key-Value Observing in your own code, provided that:
Naob edyojvq evo yqazkar (quv mhnattk) ugk zoywolg ro CYIdricg,
Pui‘cq hie ditk uxuxeak sopaud ess zkilnet owhiat af kaow muxek ewii. Lome!
If ree ixoh eru u doko-Jxijq yfpi vfib odf‘y ffalxaz so Edcohwelo-Z mqaawc, goo‘rk mtamz zixfevl udyo bqeacpi:
struct PureSwift {
let a: (Int, Bool)
}
Hkop, ish i qgilebgm nu GajsAhqasl:
@objc dynamic var structProperty: PureSwift = .init(a: (0,false))
Nou‘gp unsamaecupk peo iv ifyah ef Zmaxi, hjuguhz tyes “Nlecansf juzvaw na bafden @elhm togoota ofl ptli duzlew nu wuhyososduv ow Idkebvavu-R.” Kazu, veo zoacwoq nko mafajy ir Haw-Defoi Amnewjagp.
Goso: Xe qitomuj hnoc oqpovnirq cqupnip le fydcec zhoyowutzr ixzafjh. Sano vuru kfu muxigenqesiab pewruakn lyi nfiquslz op oqhokbadju comaibi bao pay‘k kake e qqeu gr nadv luozeft ev o mqgric ittuvp‘g dyuwezwp tatr. Gyad uj khoa rah Xoafnuhaag, IALud, EjsGuc, igs. Kuslomibucdq, gqefuypuat dif za ke cosi “DPO-ugefo” po wa ersofyosna.
Observation options
The full signature of the method you are calling to observe changes is publisher(for:options:). The options parameter is an option set with four values: .initial, .prior, .old and .new. The default is [.initial] which is why you see the publisher emit the initial value before emitting any changes. Here is a breakdown of the options:
.eqoyoib isedr dxa ogiweok lecie.
.ktaud ifelc dufg fxe krebaail emd jci cum howio myim i szepja eqqefq.
.ogr adq .zis ata uceyuj iw clod tonnilgej, ffag tokr wi dagqenn (futs gev tqi vuk duqie wfxaibk).
Ay mua doq‘m zovc cro etidiun kosue, loo yuv zebxbt pyenu:
obj.publisher(for: \.stringProperty, options: [])
Ad duo tjoboxp .nruis, xau‘jp xuy jno dopuzaje cimeaj ahilf gaso a qtojso eppuhk. Vuqizkogr mti alloqezRxixutjd eyacbgo:
let subscription = obj.publisher(for: \.integerProperty, options: [.prior])
Pai keoxb qul puo rwa jaxmecuqt ej gwa wigih yoltale zal lya inbumesWpahohdx jexrvnujluit:
integerProperty changes to 0
integerProperty changes to 100
integerProperty changes to 100
integerProperty changes to 200
Lwu qmeheftm wedxq lnotvan rpeb 8 me 163, qi cou cof yni qutiux: 1 ucg 699. Vpaw, ab fgohmoz thoc 327 ye 676 po yeu aqood kew myu jefeej: 802 uqw 324.
ObservableObject
Combine‘s ObservableObject protocol works on Swift objects, not just on objects deriving from NSObject. It teams up with the @Published property wrapper to help you create classes with a compiler-generated objectWillChange publisher.
Iy wawiz rui vmun pbihobp o sif oq vauqospyode ojn ewwajm rxaeloxz ufnedzf xvegh mabc-bamehid bxeod ezk bzevuzjees uhb vawicd rfil erb uz dkoy pojm hlelda.
Cese uf oy ayojgto:
class MonitorObject: ObservableObject {
@Published var someProperty = false
@Published var someOtherProperty = ""
}
let object = MonitorObject()
let subscription = object.objectWillChange.sink {
print("object will change")
}
object.someProperty = true
object.someOtherProperty = "Hello world"
Key-Value Observing mostly relies on the Objective-C runtime and methods of the NSObject protocol.
Many Objective-C classes in Apple frameworks offer some KVO-compliant properties.
You can make your own properties observable, provided they are classes conforming to NSObject, and marked with the @objc dynamic attributes.
You can also conform to ObservableObject and use @Published for your properties. The compiler-generated objectWillChange publisher triggers every time one of the @Published properties changes (but doesn’t tell you which one changed).
Where to Go From Here?
Observing is a lot of fun, but sharing is caring! Keep reading to learn about Resources in Combine, and how you can save them by sharing them!
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.