Let's say that we have a following code written in Swift that uses Combine:
import UIKit
import Combine
class Test {
@Published var array: [Int] = [] {
willSet {
print("willSet \(newValue.count)")
}
didSet {
print("didSet \(array.count)")
}
}
}
var test = Test()
var subscriber = test.$array.sink { values in
print("arrayCount: \(test.array.count) valuesCount: \(values.count)")
}
print("1 arrayCount \(test.array.count)")
test.array = [1, 2, 3]
print("2 arrayCount \(test.array.count)")
test.array = [1]
print("3 arrayCount \(test.array.count)")
This code prints following result on the console (it can be quickly tested in playground):
arrayCount: 0 valuesCount: 0
1 arrayCount 0
willSet 3
arrayCount: 0 valuesCount: 3
didSet 3
2 arrayCount 3
willSet 1
arrayCount: 3 valuesCount: 1
didSet 1
3 arrayCount 1
As we can see the code given to sink method is executed after willSet and before didSet of given property. Now my question is: is there any way to create this publisher or subscribe to it in such way that the code given to sink is executed after didSet and not before it (so that arrayCount and valuesCount would be the same when print from sink is executed in above example)?