Bind multiple cancellables to the lifetime of another object like your view controller.
Import Cancellor and subscribe your publishers inside an ownedCancellables
block. When your view controller is dismissed any active subscriptions will get cancelled automatically.
import Cancellor
class MyViewController: UIViewController {
override func viewDidLoad() {
ownedCancellables {
myPublisher1.sink(...)
myPublisher2.sink(...)
myPublisher3.assign(to: ..., on: ...)
}
}
}
To tie a single subscription to the lifetime of another object use:
class ViewModel: NSObject { ... }
let vm = ViewModel(...)
...
myPublisher
.sink(...)
.owned(by: vm)
Add the following dependency to your Package.swift file:
.package(url: "https://github.com/icanzilb/Cancellor", from: "0.2.0")
Cancellor is available under the MIT license. See the LICENSE file for more info.
Created by Marin Todorov.
📚 You can support me by checking out our Combine book: combinebook.com.
Inspired by Ash Furrow's NSObject-rx.
Name by https://github.com/manmal.