@@ -23,7 +23,7 @@ public class Client: NSObject {
2323 let clientKey : String ?
2424
2525 var socket : SRWebSocket ?
26- var disconnected = false
26+ var userDisconnected = false
2727
2828 // This allows us to easily plug in another request ID generation scheme, or more easily change the request id type
2929 // if needed (technically this could be a string).
@@ -119,15 +119,15 @@ extension Client {
119119
120120 - parameter query: The query to register for updates.
121121 - parameter subclassType: The subclass of PFObject to be used as the type of the Subscription.
122- This parameter can be automatically inferred from context most of the time
122+ This parameter can be automatically inferred from context most of the time
123123
124124 - returns: The subscription that has just been registered
125125 */
126126 public func subscribe< T where T: PFObject > (
127127 query: PFQuery ,
128128 subclassType: T . Type = T . self
129129 ) -> Subscription < T > {
130- return subscribe ( query, handler: Subscription < T > ( ) )
130+ return subscribe ( query, handler: Subscription < T > ( ) )
131131 }
132132
133133 /**
@@ -142,22 +142,24 @@ extension Client {
142142 query: PFQuery ,
143143 handler: T
144144 ) -> T {
145- let subscriptionRecord = SubscriptionRecord (
146- query: query,
147- requestId: requestIdGenerator ( ) ,
148- handler: handler
149- )
150- subscriptions. append ( subscriptionRecord)
151-
152- if socket == nil {
153- if !disconnected {
154- reconnect ( )
155- }
145+ let subscriptionRecord = SubscriptionRecord (
146+ query: query,
147+ requestId: requestIdGenerator ( ) ,
148+ handler: handler
149+ )
150+ subscriptions. append ( subscriptionRecord)
151+
152+ if socket? . readyState == . OPEN {
153+ sendOperationAsync ( . Subscribe( requestId: subscriptionRecord. requestId, query: query) )
154+ } else if socket == nil || socket? . readyState != . CONNECTING {
155+ if !userDisconnected {
156+ reconnect ( )
156157 } else {
157- sendOperationAsync ( . Subscribe ( requestId : subscriptionRecord . requestId , query : query ) )
158+ debugPrint ( " Warning: The client was explicitly disconnected! You must explicitly call .reconnect() in order to process your subscriptions. " )
158159 }
159-
160- return handler
160+ }
161+
162+ return handler
161163 }
162164
163165 /**
@@ -183,10 +185,11 @@ extension Client {
183185 func unsubscribe( @noescape matching matcher: SubscriptionRecord -> Bool ) {
184186 subscriptions. filter {
185187 matcher ( $0)
186- } . forEach {
187- sendOperationAsync ( . Unsubscribe( requestId: $0. requestId) )
188+ } . forEach {
189+ sendOperationAsync ( . Unsubscribe( requestId: $0. requestId) )
188190 }
189191 }
192+
190193}
191194
192195extension Client {
@@ -203,7 +206,7 @@ extension Client {
203206 socket. delegate = self
204207 socket. setDelegateDispatchQueue ( queue)
205208 socket. open ( )
206-
209+ userDisconnected = false
207210 return socket
208211 } ( )
209212 }
@@ -221,6 +224,6 @@ extension Client {
221224 }
222225 socket. close ( )
223226 self . socket = nil
224- disconnected = true
227+ userDisconnected = true
225228 }
226- }
229+ }
0 commit comments