I found a lot of SwiftUI-related topics about this which didn't help (eg Why an ObservedObject array is not updated in my SwiftUI application?)
This doesn't work with Combine in Swift (specifically not using SwiftUI):
class SomeTask {
@Published var progress = Progress(totalUnitCount: 5) // Progress is a Class
[...]
}
var task = SomeTask()
let cancellable = task.$progress.sink { print($0.fractionCompleted) }
task.progress.completedUnitCount = 2
This is not SwiftUI-related so no ObservableObject
inheritance to get objectWillChange
, but even if I try to use ObservableObject
and task.objectWillChange.send()
it doesn't do anything, also trying to add extension Progress: ObservableObject {}
doesn't help.
Since the publisher emits values through the var's willSet
and since Progress
is itself class-type nothing happens.
Looks like there is no real decent way to manually trigger it?
Only solution I found is to just re-assign itself which is quite awkward:
let pr = progress
progress = pr
(writing progress = progress
is a compile-time error).
Only other way which might be working is probably by using Key-value-observing/KVO and/or writing a new @PublishedClassType
property wrapper?