Skip to content

Commit d064eab

Browse files
committed
Make self in reachability closures weak instead of unowned
See #116
1 parent b1a42b6 commit d064eab

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Source/PusherConnection.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,24 @@ open class PusherConnection: NSObject {
3838

3939
open lazy var reachability: Reachability? = {
4040
let reachability = Reachability.init()
41-
reachability?.whenReachable = { [unowned self] reachability in
42-
self.delegate?.debugLog?(message: "[PUSHER DEBUG] Network reachable")
43-
if self.connectionState == .disconnected || self.connectionState == .reconnectingWhenNetworkBecomesReachable {
44-
self.attemptReconnect()
41+
reachability?.whenReachable = { [weak self] reachability in
42+
guard self != nil else {
43+
print("Your Pusher instance has probably become deallocated. See https://github.com/pusher/pusher-websocket-swift/issues/109 for more information")
44+
return
45+
}
46+
47+
self!.delegate?.debugLog?(message: "[PUSHER DEBUG] Network reachable")
48+
if self!.connectionState == .disconnected || self!.connectionState == .reconnectingWhenNetworkBecomesReachable {
49+
self!.attemptReconnect()
4550
}
4651
}
47-
reachability?.whenUnreachable = { [unowned self] reachability in
48-
self.delegate?.debugLog?(message: "[PUSHER DEBUG] Network unreachable")
52+
reachability?.whenUnreachable = { [weak self] reachability in
53+
guard self != nil else {
54+
print("Your Pusher instance has probably become deallocated. See https://github.com/pusher/pusher-websocket-swift/issues/109 for more information")
55+
return
56+
}
57+
58+
self!.delegate?.debugLog?(message: "[PUSHER DEBUG] Network unreachable")
4959
}
5060
return reachability
5161
}()

0 commit comments

Comments
 (0)