CombineにRxSwiftのDisposeBagのような機能のものはありますか?
下記のようなコードだと、Future呼び出しごとに AnyCancellable
の変数を用意しないといけません。
import Foundation
import Combine
class Hoge {
private var cancellable1: AnyCancellable?
private var cancellable2: AnyCancellable?
func foo() {
self.cancellable1 = createFuture().sink(
receiveValue: { v in
print(v)
}
)
self.cancellable2 = createFuture().sink(
receiveValue: { v in
print(v)
}
)
}
private func createFuture() -> Future<Int, Never> {
return Future { promise in
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
promise(.success(10))
}
}
}
}