2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Combine] 複数のAnyCancellableをまとめてstoreする

Posted at

をCombineでもやりたいよねって話。

ただし、Combineの場合 Swift Foundation の Set が使われており DisposeBag.insert(_:) に相当するfuncが存在しないので自前で用意する必要があります。

Set は struct なので mutating を付けるのがミソ。

public extension Set where Element == AnyCancellable {
    mutating func insert(_ cancellables: [AnyCancellable]) {
        cancellables.forEach {
            $0.store(in: &self)
        }
    }

    mutating func insert(_ cancellables: AnyCancellable...) {
        insert(cancellables)
    }
}

これで

hoge.sink { ~ }
    .store(in: &cancellables)
fuga.sink { ~ }
    .store(in: &cancellables)

cancellables.insert(
    hoge.sink { ~ },
    fuga.sink { ~ }
)

と書けるようになりました🎉

折角なのでresultBuilderも作っちゃいましょう。

public extension Set where Element == AnyCancellable {
    /// Convenience function allows a list of cancellables to be gathered for cancel.
    mutating func insert(@AnyCancellableBuilder _ builder: () -> [AnyCancellable]) {
        insert(builder())
    }
    
    @resultBuilder
    struct AnyCancellableBuilder {
        static func buildBlock(_ cancellables: AnyCancellable...) -> [AnyCancellable] {
            cancellables
        }
    }
}

これでRxSwift時代と同じように書けるぞい💪('ω'💪)

cancellables.insert {
    hoge.sink { ~ }
    fuga.sink { ~ }
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?