Skip to content

Commit 130f415

Browse files
committed
cancel web socket task on deinit and add more logs
1 parent cfbf119 commit 130f415

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

AmplifyPlugins/Predictions/AWSPredictionsPlugin/Liveness/Service/WebSocketSession.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ final class WebSocketSession {
3131
)
3232
}
3333

34+
deinit {
35+
Amplify.log.verbose("\(#fileID)-\(#function)")
36+
task?.cancel(with: .normalClosure, reason: nil)
37+
}
38+
3439
func onMessageReceived(_ receive: @escaping (Result<URLSessionWebSocketTask.Message, Error>) -> WebSocketMessageResult) {
3540
receiveMessage = receive
3641
}
@@ -92,11 +97,15 @@ final class WebSocketSession {
9297
)
9398
}
9499

95-
final class Delegate: NSObject, URLSessionWebSocketDelegate, URLSessionTaskDelegate {
100+
final class Delegate: NSObject, URLSessionWebSocketDelegate {
96101
var onClose: (URLSessionWebSocketTask.CloseCode) -> Void = { _ in }
97102
var onOpen: () -> Void = {}
98103
var onServerDateReceived: (Date?) -> Void = { _ in }
99104

105+
deinit {
106+
Amplify.log.verbose("\(#fileID).Delegate-\(#function)")
107+
}
108+
100109
// MARK: - URLSessionWebSocketDelegate methods
101110
func urlSession(
102111
_ session: URLSession,
@@ -150,6 +159,14 @@ final class WebSocketSession {
150159
) {
151160
Amplify.log.verbose("\(#fileID)-\(#function): Session task didCompleteWithError : \(error)")
152161
}
162+
163+
// MARK: - URLSessionDelegate methods
164+
func urlSession(
165+
_ session: URLSession,
166+
didBecomeInvalidWithError error: Error?
167+
) {
168+
Amplify.log.verbose("\(#fileID)-\(#function): Session task didBecomeInvalidWithError : \(error)")
169+
}
153170
}
154171

155172
enum WebSocketMessageResult {

0 commit comments

Comments
 (0)