Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit 71cfa0a

Browse files
ananfangflovilmart
authored andcommitted
Add a property "shouldPrintWebSocketLog" for Client (#126)
* turn off web socket log in default. * Safe way to access shouldPrintWebSocketLog of Client in block. * WebSocket log will be printed in default. Set shouldPrintWebSocketLog to false to turn it off * Make shouldPrintWebSocketLog as a public property * Add description for shouldPrintWebSocketLog in README
1 parent 629a053 commit 71cfa0a

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ subscription.handle(Event.created) { query, object in
5454
}
5555
```
5656

57+
By default, it will print the logs from WebSocket / WebSocketDelegate. You can turn it off:
58+
```swift
59+
Client.shared.shouldPrintWebSocketLog = false
60+
```
61+
5762
Handling errors is and other events is similar, take a look at the `Subscription` class for more information.
5863

5964
## Advanced Usage
@@ -66,7 +71,7 @@ We want to make contributing to this project as easy and transparent as possible
6671

6772
-----
6873

69-
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.
74+
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.
7075

7176
[releases]: https://github.com/parse-community/ParseLiveQuery-iOS-OSX/releases
7277
[contributing]: https://github.com/parse-community/ParseLiveQuery-iOS-OSX/blob/master/CONTRIBUTING.md
@@ -88,4 +93,3 @@ As of April 5, 2017, Parse, LLC has transferred this code to the parse-community
8893

8994
[carthage-svg]:https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat
9095
[carthage-link]:https://github.com/Carthage/Carthage
91-

Sources/ParseLiveQuery/Client.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ open class Client: NSObject {
2323
let clientKey: String?
2424

2525
var socket: WebSocket?
26+
public var shouldPrintWebSocketLog = true
2627
public var userDisconnected = false
2728

2829
// This allows us to easily plug in another request ID generation scheme, or more easily change the request id type

Sources/ParseLiveQuery/Internal/ClientPrivate.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ func == (first: Client.RequestId, second: Client.RequestId) -> Bool {
117117
extension Client: WebSocketDelegate {
118118

119119
public func websocketDidReceiveData(socket: WebSocket, data: Data) {
120-
print("Received binary data but we don't handle it...")
120+
if shouldPrintWebSocketLog { print("Received binary data but we don't handle it...") }
121121
}
122122

123123
public func websocketDidReceiveMessage(socket: WebSocket, text: String) {
124-
handleOperationAsync(text).continueWith { task in
125-
if let error = task.error {
124+
handleOperationAsync(text).continueWith { [weak self] task in
125+
if let error = task.error, self?.shouldPrintWebSocketLog == true {
126126
print("Error: \(error)")
127127
}
128128
}
@@ -134,7 +134,7 @@ extension Client: WebSocketDelegate {
134134
}
135135

136136
public func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
137-
print("error: \(error)")
137+
if shouldPrintWebSocketLog { print("error: \(String(describing: error))") }
138138

139139
// TODO: Better retry logic, unless `disconnect()` was explicitly called
140140
if !userDisconnected {
@@ -143,7 +143,7 @@ extension Client: WebSocketDelegate {
143143
}
144144

145145
public func webSocket(_ webSocket: WebSocket, didCloseWithCode code: Int, reason: String?, wasClean: Bool) {
146-
print("code: \(code) reason: \(reason)")
146+
if shouldPrintWebSocketLog { print("code: \(code) reason: \(String(describing: reason))") }
147147

148148
// TODO: Better retry logic, unless `disconnect()` was explicitly called
149149
if !userDisconnected {

0 commit comments

Comments
 (0)