From 0d25ef84b2a5902ec4486a176343039ae7e274ac Mon Sep 17 00:00:00 2001 From: Sebastian Thiebaud Date: Wed, 23 May 2018 09:38:34 -0700 Subject: [PATCH] PR changes --- Sources/SwiftGRPC/Core/Channel.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/SwiftGRPC/Core/Channel.swift b/Sources/SwiftGRPC/Core/Channel.swift index be8d7640b..5cd49e753 100644 --- a/Sources/SwiftGRPC/Core/Channel.swift +++ b/Sources/SwiftGRPC/Core/Channel.swift @@ -73,6 +73,7 @@ public class Channel { } deinit { + connectivityObservers.forEach { $0.shutdown() } cgrpc_channel_destroy(underlyingChannel) completionQueue.shutdown() } @@ -101,8 +102,7 @@ public class Channel { /// /// - Parameter callback: block executed every time a new connectivity state is detected public func subscribe(callback: @escaping (ConnectivityState) -> Void) { - let observer = ConnectivityObserver(underlyingChannel: underlyingChannel, currentState: connectivityState(), callback: callback) - connectivityObservers.append(observer) + connectivityObservers.append(ConnectivityObserver(underlyingChannel: underlyingChannel, currentState: connectivityState(), callback: callback)) } } @@ -124,15 +124,13 @@ private extension Channel { } deinit { - completionQueue.shutdown() + shutdown() } private func run() { let spinloopThreadQueue = DispatchQueue(label: "SwiftGRPC.ConnectivityObserver.run.spinloopThread") - spinloopThreadQueue.async { [weak self] in - guard let `self` = self else { return } - + spinloopThreadQueue.async { while true { guard let underlyingState = self.lastState.underlyingState else { return } @@ -159,6 +157,10 @@ private extension Channel { } } } + + func shutdown() { + completionQueue.shutdown() + } } }