I am faced with the problem of migration from ObservableObject+Combine to Observable.
Apple has a great migration guide, and it doesn't even raise any questions. But there is no information on how to save the code from Combine.
Here is an example of a class with the ObservableObject protocol:
class ViewModel: ObservableObject {
@Published var scale: CGFloat = 1.0
var cancellable: Set<AnyCancellable> = []
init() {
$scale
.dropFirst()
.debounce(for: 0.2, scheduler: RunLoop.main)
.sink { value in
Task(){
// Code
}
}
.store(in: &cancellable)
}
}
The question is as follows - how to save constructions related to working with Combine like .sink{}, .debounce(), .store(), etc.?
I didn't find it anywhere in the documentation. So far I see only to paint the functionality using .get {} .set{}
These things are very convenient and I use them in several commercial projects. Without this, it is difficult to maintain the normal operation of business logic.
Maybe someone has encountered this?
First and foremost, there is no reason you have to give up
@PublishedandObservableObjectif you like the pattern's semantics and are making use of it. Those technologies are built usingCombineas an implementation detail, and it seems that you are making use of that.The new
@Observabledoes have a slightly different model. I can't find a way to expand the macro but it looks like it injects anObservationRegistrardirectly into the class and I surmise it cascades calls the registrar on classes in a notification chain instead of relying on aCombineobservable.You may also want to consider making your use of
Combinemore explicit. Realizing that it's just sample code, the@Publishedproperty in your sample could be replaced by aCurrentValueSubjectfor much the same effect.