Ensure unused publishers are canceled

This commit is contained in:
Justin Mazzocchi 2021-01-27 18:05:15 -08:00
parent 555d436cf5
commit aa08b9b284
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -14,6 +14,7 @@ public final class RootViewModel: ObservableObject {
private let userNotificationService: UserNotificationService private let userNotificationService: UserNotificationService
private let registerForRemoteNotifications: () -> AnyPublisher<Data, Error> private let registerForRemoteNotifications: () -> AnyPublisher<Data, Error>
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
private var navigationViewModelCancellable: AnyCancellable?
public init(environment: AppEnvironment, public init(environment: AppEnvironment,
registerForRemoteNotifications: @escaping () -> AnyPublisher<Data, Error>) throws { registerForRemoteNotifications: @escaping () -> AnyPublisher<Data, Error>) throws {
@ -95,7 +96,7 @@ private extension RootViewModel {
} }
.share() .share()
identityPublisher navigationViewModelCancellable = identityPublisher
.filter { [weak self] in $0.id != self?.navigationViewModel?.identityContext.identity.id } .filter { [weak self] in $0.id != self?.navigationViewModel?.identityContext.identity.id }
.map { [weak self] in .map { [weak self] in
guard let self = self else { return nil } guard let self = self else { return nil }
@ -121,6 +122,6 @@ private extension RootViewModel {
return NavigationViewModel(identityContext: identityContext) return NavigationViewModel(identityContext: identityContext)
} }
.assign(to: &$navigationViewModel) .sink { [weak self] in self?.navigationViewModel = $0 }
} }
} }