@@ -32,6 +32,7 @@ class RTClient {
3232 private var socket : SocketIOClient ?
3333 private var socketManager : SocketManager ?
3434 private var subscriptions : [ String : RTSubscription ]
35+ private var subscribedOnConnect = [ String] ( )
3536 private var eventSubscriptions : [ String : [ RTSubscription ] ]
3637 private var methods : [ String : RTMethodRequest ]
3738 var socketCreated = false
@@ -41,7 +42,6 @@ class RTClient {
4142 private var onResultReady = false
4243 private var onMethodResultReady = false
4344 private var onDisconnectCalledOnce = false
44- private var _lock : NSLock
4545 private var reconnectAttempt : Int = 1
4646 private var timeInterval : Double = 0.2 // seconds
4747 var onSocketConnectCallback : ( ( ) -> Void ) ?
@@ -51,9 +51,9 @@ class RTClient {
5151 private init ( ) {
5252 self . waitingSubscriptions = [ RTSubscription] ( )
5353 self . subscriptions = [ String : RTSubscription] ( )
54+ self . subscribedOnConnect = [ String] ( )
5455 self . eventSubscriptions = [ String : [ RTSubscription] ] ( )
5556 self . methods = [ String : RTMethodRequest] ( )
56- _lock = NSLock ( )
5757 }
5858
5959 func connectSocket( connected: ( ( ) -> Void ) ! ) {
@@ -68,11 +68,11 @@ class RTClient {
6868 let path = " / " + Backendless. shared. getApplicationId ( )
6969
7070 var clientId = " "
71- #if os(iOS) || os(tvOS)
71+ #if os(iOS) || os(tvOS)
7272 clientId = DeviceHelper . shared. deviceId
73- #elseif os(OSX)
73+ #elseif os(OSX)
7474 clientId = DeviceHelper . shared. deviceId
75- #endif
75+ #endif
7676
7777 var connectParams = [ " apiKey " : Backendless . shared. getApiKey ( ) , " clientId " : clientId]
7878 if let userToken = UserDefaultsHelper . shared. getUserToken ( ) {
@@ -88,7 +88,7 @@ class RTClient {
8888 let _ = Backendless . shared. rt. addConnectErrorEventListener ( responseHandler: { _ in
8989 self . tryToReconnectSocket ( )
9090 } )
91-
91+
9292 if self . socket != nil {
9393 self . socketOnceCreated = true
9494 self . socketCreated = true
@@ -119,23 +119,22 @@ class RTClient {
119119 }
120120
121121 func subscribe( data: [ String : Any ] , subscription: RTSubscription ) {
122- if self . socketConnected {
123- self . socket? . emit ( " SUB_ON " , data)
124- }
125- else {
126- self . connectSocket ( connected: {
127- self . socket? . emit ( " SUB_ON " , data)
128- } )
129- }
130122 if !self . needResubscribe {
131123 self . subscriptions [ subscription. subscriptionId!] = subscription
132124 }
125+ if !self . socketConnected {
126+ self . connectSocket ( connected: { } )
127+ }
128+ else if !self . subscribedOnConnect. contains ( subscription. subscriptionId!) {
129+ self . socket? . emit ( " SUB_ON " , data)
130+ }
133131 }
134132
135133 func unsubscribe( subscriptionId: String ) {
136134 if self . subscriptions. keys. contains ( subscriptionId) {
137135 self . socket? . emit ( " SUB_OFF " , [ " id " : subscriptionId] )
138136 self . subscriptions. removeValue ( forKey: subscriptionId)
137+ self . subscribedOnConnect. removeAll { $0 == subscriptionId }
139138 }
140139 else {
141140 for type in self . eventSubscriptions. keys {
@@ -180,15 +179,24 @@ class RTClient {
180179 let type = subscription. type,
181180 let options = subscription. options {
182181 let data = [ " id " : subscriptionId, " name " : type, " options " : options] as [ String : Any ]
183- self . subscribe ( data : data , subscription : subscription )
182+ self . socket ? . emit ( " SUB_ON " , data )
184183 }
185184 }
186185 self . needResubscribe = false
187186 }
188187 else if !self . needResubscribe {
189188 connected ( )
189+ for subscriptionId in self . subscriptions. keys {
190+ if let subscription = self . subscriptions [ subscriptionId] ,
191+ let type = subscription. type,
192+ let options = subscription. options {
193+ let data = [ " id " : subscriptionId, " name " : type, " options " : options] as [ String : Any ]
194+ self . socket? . emit ( " SUB_ON " , data)
195+ self . subscribedOnConnect. append ( subscriptionId)
196+ }
197+ }
190198 }
191-
199+
192200 self . onResult ( )
193201 self . onMethodResult ( )
194202
@@ -201,7 +209,7 @@ class RTClient {
201209 self . subscribeForObjectChangesWaiting ( )
202210 } )
203211
204- self . socket? . on ( " connect_error " , callback: { data, ack in
212+ self . socket? . on ( " connect_error " , callback: { data, ack in
205213 if let reason = data. first as? String {
206214 self . onConnectErrorOrDisconnect ( reason: reason, type: ConnectEvents . connectError)
207215 }
@@ -245,7 +253,7 @@ class RTClient {
245253 for subscription in reconnectAttemptSubscriptions {
246254 let attempt = NSNumber ( value: self . reconnectAttempt)
247255 let timeout = NSNumber ( value: maxTimeInterval * 1000 )
248- let reconnectAttemptObject = ReconnectAttemptObject ( attempt: attempt, timeout: timeout)
256+ let reconnectAttemptObject = ReconnectAttemptObject ( attempt: attempt, timeout: timeout)
249257 subscription. onResult!( reconnectAttemptObject)
250258 }
251259 }
@@ -274,12 +282,12 @@ class RTClient {
274282 func onResult( ) {
275283 if !self . onResultReady {
276284 self . socket? . on ( " SUB_RES " , callback: { data, ack in
277- self . onResultReady = true
285+ self . onResultReady = true
278286 if let resultData = data. first as? [ String : Any ] ,
279287 let subscriptionId = resultData [ " id " ] as? String ,
280288 let subscription = self . subscriptions [ subscriptionId] {
281289
282- if let result = resultData [ " data " ] {
290+ if let result = resultData [ " data " ] {
283291 subscription. ready = true
284292 if let result = result as? String , result == " connected " , subscription. onConnect != nil {
285293 subscription. onConnect!( )
@@ -331,7 +339,7 @@ class RTClient {
331339 self . methods. removeValue ( forKey: methodId)
332340 }
333341 }
334- else if resultData [ " id " ] != nil , method. onResult != nil {
342+ else if resultData [ " id " ] != nil , method. onResult != nil {
335343 if let result = resultData [ " result " ] {
336344 method. onResult!( result)
337345 }
@@ -361,7 +369,7 @@ class RTClient {
361369 subscriptions = [ RTSubscription] ( )
362370 }
363371 subscriptions? . append ( subscription)
364- eventSubscriptions [ type] = subscriptions
372+ eventSubscriptions [ type] = subscriptions
365373 }
366374
367375 func getSimpleListeners( type: String ) -> [ RTSubscription ] ? {
@@ -391,7 +399,7 @@ class RTClient {
391399
392400 subscriptions? . append ( subscription)
393401 eventSubscriptions [ type] = subscriptions
394- return subscription
402+ return subscription
395403 }
396404
397405 func removeEventListeners( type: String ) {
@@ -427,8 +435,8 @@ class RTClient {
427435 var indexesToRemove = [ Int] ( ) // waiting subscriptions will be removed after subscription is done
428436 for waitingSubscription in waitingSubscriptions {
429437 if let data = waitingSubscription. data,
430- let name = data [ " name " ] as? String ,
431- ( name == RtTypes . objectsChanges || name == RtTypes . relationsChanges) {
438+ let name = data [ " name " ] as? String ,
439+ ( name == RtTypes . objectsChanges || name == RtTypes . relationsChanges) {
432440 waitingSubscription. subscribe ( )
433441 indexesToRemove. append ( waitingSubscriptions. firstIndex ( of: waitingSubscription) !)
434442 }
0 commit comments