Skip to content

Commit 8e5cc29

Browse files
authored
Merge pull request #199 from PorterHoskins/self-retain-reachability
Capture self reference before using self reference in Reachability closures
2 parents 4e927a8 + 34276d6 commit 8e5cc29

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Sources/PusherConnection.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,42 +42,42 @@ public typealias PusherEventJSON = [String: AnyObject]
4242
open lazy var reachability: Reachability? = {
4343
let reachability = Reachability.init()
4444
reachability?.whenReachable = { [weak self] reachability in
45-
guard self != nil else {
45+
guard let strongSelf = self else {
4646
print("Your Pusher instance has probably become deallocated. See https://github.com/pusher/pusher-websocket-swift/issues/109 for more information")
4747
return
4848
}
4949

50-
self!.delegate?.debugLog?(message: "[PUSHER DEBUG] Network reachable")
50+
strongSelf.delegate?.debugLog?(message: "[PUSHER DEBUG] Network reachable")
5151

52-
switch self!.connectionState {
52+
switch strongSelf.connectionState {
5353
case .disconnecting, .connecting, .reconnecting:
5454
// If in one of these states then part of the connection, reconnection, or explicit
5555
// disconnection process is underway, so do nothing
5656
return
5757
case .disconnected:
5858
// If already disconnected then reset connection and try to reconnect, provided the
5959
// state isn't disconnected because of an intentional disconnection
60-
if !self!.intentionalDisconnect { self!.resetConnectionAndAttemptReconnect() }
60+
if !strongSelf.intentionalDisconnect { strongSelf.resetConnectionAndAttemptReconnect() }
6161
return
6262
case .connected:
6363
// If already connected then we assume that there was a missed network event that
6464
// led to a bad connection so we move to the disconnected state and then attempt
6565
// reconnection
66-
self!.delegate?.debugLog?(
66+
strongSelf.delegate?.debugLog?(
6767
message: "[PUSHER DEBUG] Connection state is \(self!.connectionState.stringValue()) but received network reachability change so going to call attemptReconnect"
6868
)
69-
self!.resetConnectionAndAttemptReconnect()
69+
strongSelf.resetConnectionAndAttemptReconnect()
7070
return
7171
}
7272
}
7373
reachability?.whenUnreachable = { [weak self] reachability in
74-
guard self != nil else {
74+
guard let strongSelf = self else {
7575
print("Your Pusher instance has probably become deallocated. See https://github.com/pusher/pusher-websocket-swift/issues/109 for more information")
7676
return
7777
}
7878

79-
self!.delegate?.debugLog?(message: "[PUSHER DEBUG] Network unreachable")
80-
self!.resetConnectionAndAttemptReconnect()
79+
strongSelf.delegate?.debugLog?(message: "[PUSHER DEBUG] Network unreachable")
80+
strongSelf.resetConnectionAndAttemptReconnect()
8181
}
8282
return reachability
8383
}()

0 commit comments

Comments
 (0)