From d7e17d23ef7dcdc3e024cea8445c13eedd5be193 Mon Sep 17 00:00:00 2001 From: ananfang Date: Fri, 21 Jul 2017 17:44:59 +0800 Subject: [PATCH 1/5] turn off web socket log in default. --- Sources/ParseLiveQuery/Client.swift | 1 + Sources/ParseLiveQuery/Internal/ClientPrivate.swift | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/ParseLiveQuery/Client.swift b/Sources/ParseLiveQuery/Client.swift index 370ba277..b53761a3 100644 --- a/Sources/ParseLiveQuery/Client.swift +++ b/Sources/ParseLiveQuery/Client.swift @@ -23,6 +23,7 @@ open class Client: NSObject { let clientKey: String? var socket: WebSocket? + var shouldPrintWebSocketLog = false public var userDisconnected = false // This allows us to easily plug in another request ID generation scheme, or more easily change the request id type diff --git a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift index 456b38bd..9596abc7 100644 --- a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift +++ b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift @@ -117,12 +117,12 @@ func == (first: Client.RequestId, second: Client.RequestId) -> Bool { extension Client: WebSocketDelegate { public func websocketDidReceiveData(socket: WebSocket, data: Data) { - print("Received binary data but we don't handle it...") + if shouldPrintWebSocketLog { print("Received binary data but we don't handle it...") } } public func websocketDidReceiveMessage(socket: WebSocket, text: String) { handleOperationAsync(text).continueWith { task in - if let error = task.error { + if let error = task.error, shouldPrintWebSocketLog { print("Error: \(error)") } } @@ -134,7 +134,7 @@ extension Client: WebSocketDelegate { } public func websocketDidDisconnect(socket: WebSocket, error: NSError?) { - print("error: \(error)") + if shouldPrintWebSocketLog { print("error: \(String(describing: error))") } // TODO: Better retry logic, unless `disconnect()` was explicitly called if !userDisconnected { @@ -143,7 +143,7 @@ extension Client: WebSocketDelegate { } public func webSocket(_ webSocket: WebSocket, didCloseWithCode code: Int, reason: String?, wasClean: Bool) { - print("code: \(code) reason: \(reason)") + if shouldPrintWebSocketLog { print("code: \(code) reason: \(String(describing: reason))") } // TODO: Better retry logic, unless `disconnect()` was explicitly called if !userDisconnected { From aafddea10de9343ebc1fef8cef3712c3bfbc906f Mon Sep 17 00:00:00 2001 From: ananfang Date: Fri, 21 Jul 2017 17:57:50 +0800 Subject: [PATCH 2/5] Safe way to access shouldPrintWebSocketLog of Client in block. --- Sources/ParseLiveQuery/Internal/ClientPrivate.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift index 9596abc7..12963b9c 100644 --- a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift +++ b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift @@ -121,8 +121,8 @@ extension Client: WebSocketDelegate { } public func websocketDidReceiveMessage(socket: WebSocket, text: String) { - handleOperationAsync(text).continueWith { task in - if let error = task.error, shouldPrintWebSocketLog { + handleOperationAsync(text).continueWith { [weak self] task in + if let error = task.error, self?.shouldPrintWebSocketLog == true { print("Error: \(error)") } } From 6e4e300550f1479814480bffd3153485f173d3c5 Mon Sep 17 00:00:00 2001 From: ananfang Date: Fri, 21 Jul 2017 18:04:34 +0800 Subject: [PATCH 3/5] WebSocket log will be printed in default. Set shouldPrintWebSocketLog to false to turn it off --- Sources/ParseLiveQuery/Client.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ParseLiveQuery/Client.swift b/Sources/ParseLiveQuery/Client.swift index b53761a3..5ebcffcc 100644 --- a/Sources/ParseLiveQuery/Client.swift +++ b/Sources/ParseLiveQuery/Client.swift @@ -23,7 +23,7 @@ open class Client: NSObject { let clientKey: String? var socket: WebSocket? - var shouldPrintWebSocketLog = false + var shouldPrintWebSocketLog = true public var userDisconnected = false // This allows us to easily plug in another request ID generation scheme, or more easily change the request id type From 0c5b2626869a69d5619275e42f3e449cf1f0c4ec Mon Sep 17 00:00:00 2001 From: ananfang Date: Fri, 21 Jul 2017 18:19:48 +0800 Subject: [PATCH 4/5] Make shouldPrintWebSocketLog as a public property --- Sources/ParseLiveQuery/Client.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ParseLiveQuery/Client.swift b/Sources/ParseLiveQuery/Client.swift index 5ebcffcc..a5d98538 100644 --- a/Sources/ParseLiveQuery/Client.swift +++ b/Sources/ParseLiveQuery/Client.swift @@ -23,7 +23,7 @@ open class Client: NSObject { let clientKey: String? var socket: WebSocket? - var shouldPrintWebSocketLog = true + public var shouldPrintWebSocketLog = true public var userDisconnected = false // This allows us to easily plug in another request ID generation scheme, or more easily change the request id type From ba559a068d8ae51c4aac045212f6c0aded53fd5a Mon Sep 17 00:00:00 2001 From: ananfang Date: Fri, 21 Jul 2017 18:34:09 +0800 Subject: [PATCH 5/5] Add description for shouldPrintWebSocketLog in README --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4737c976..5fde91af 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,11 @@ subscription.handle(Event.Created) { query, object in } ``` +By default, it will print the logs from WebSocket / WebSocketDelegate. You can turn it off: +```swift +Client.shared.shouldPrintWebSocketLog = false +``` + Handling errors is and other events is similar, take a look at the `Subscription` class for more information. ## Advanced Usage @@ -65,7 +70,7 @@ We want to make contributing to this project as easy and transparent as possible ----- -As of April 5, 2017, Parse, LLC has transferred this code to the parse-community organization, and will no longer be contributing to or distributing this code. +As of April 5, 2017, Parse, LLC has transferred this code to the parse-community organization, and will no longer be contributing to or distributing this code. [releases]: https://github.com/parse-community/ParseLiveQuery-iOS-OSX/releases [contributing]: https://github.com/parse-community/ParseLiveQuery-iOS-OSX/blob/master/CONTRIBUTING.md @@ -87,4 +92,3 @@ As of April 5, 2017, Parse, LLC has transferred this code to the parse-community [carthage-svg]:https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat [carthage-link]:https://github.com/Carthage/Carthage -