Skip to content

Commit 72fc22b

Browse files
committed
Add to subscription list before removing from pending list
1 parent b710984 commit 72fc22b

File tree

2 files changed

+105
-24
lines changed

2 files changed

+105
-24
lines changed

ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift

Lines changed: 103 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct GameScore: ParseObject {
4343
//: Be sure you have LiveQuery enabled on your server.
4444

4545
//: Create a query just as you normally would.
46-
var query = GameScore.query("score" > 9)
46+
var query = GameScore.query("score" < 11)
4747

4848
#if canImport(SwiftUI)
4949
//: To use subscriptions inside of SwiftUI
@@ -61,7 +61,7 @@ struct ContentView: View {
6161
Text("Unsubscribed from query!")
6262
} else if let event = subscription.event {
6363

64-
//: This is how you register to receive notificaitons of events related to your LiveQuery.
64+
//: This is how you register to receive notifications of events related to your LiveQuery.
6565
switch event.event {
6666

6767
case .entered(let object):
@@ -116,7 +116,7 @@ subscription.handleSubscribe { subscribedQuery, isNew in
116116
}
117117
}
118118

119-
//: This is how you register to receive notificaitons of events related to your LiveQuery.
119+
//: This is how you register to receive notifications of events related to your LiveQuery.
120120
subscription.handleEvent { _, event in
121121
switch event {
122122

@@ -133,10 +133,19 @@ subscription.handleEvent { _, event in
133133
}
134134
}
135135

136+
//: Ping the LiveQuery server
137+
ParseLiveQuery.client?.sendPing { error in
138+
if let error = error {
139+
print("Error pinging LiveQuery server: \(error)")
140+
} else {
141+
print("Successfully pinged server!")
142+
}
143+
}
144+
136145
//: Now go to your dashboard, go to the GameScore table and add, update or remove rows.
137146
//: You should receive notifications for each.
138147

139-
//: This is how you register to receive notificaitons about being unsubscribed.
148+
//: This is how you register to receive notifications about being unsubscribed.
140149
subscription.handleUnsubscribe { query in
141150
print("Unsubscribed from \(query)")
142151
}
@@ -148,7 +157,8 @@ do {
148157
print(error)
149158
}
150159

151-
//: Ping the LiveQuery server
160+
//: Ping the LiveQuery server. This should produce an error
161+
//: because LiveQuery is disconnected.
152162
ParseLiveQuery.client?.sendPing { error in
153163
if let error = error {
154164
print("Error pinging LiveQuery server: \(error)")
@@ -157,12 +167,6 @@ ParseLiveQuery.client?.sendPing { error in
157167
}
158168
}
159169

160-
//: To close the current LiveQuery connection.
161-
ParseLiveQuery.client?.close()
162-
163-
//: To close all LiveQuery connections.
164-
ParseLiveQuery.client?.closeAll()
165-
166170
//: Create a new query.
167171
var query2 = GameScore.query("score" > 50)
168172

@@ -199,11 +203,86 @@ subscription2.handleEvent { _, event in
199203
}
200204
}
201205

202-
//: Now go to your dashboard, go to the GameScore table and add, update or remove rows.
203-
//: You should receive notifications for each, but only with your fields information.
206+
//: To close the current LiveQuery connection.
207+
ParseLiveQuery.client?.close()
208+
209+
//: To close all LiveQuery connections use:
210+
//ParseLiveQuery.client?.closeAll()
204211

205-
//: This is how you register to receive notificaitons about being unsubscribed.
206-
subscription2.handleUnsubscribe { query in
212+
//: Ping the LiveQuery server. This should produce an error
213+
//: because LiveQuery is disconnected.
214+
ParseLiveQuery.client?.sendPing { error in
215+
if let error = error {
216+
print("Error pinging LiveQuery server: \(error)")
217+
} else {
218+
print("Successfully pinged server!")
219+
}
220+
}
221+
222+
//: Subscribe to your new query.
223+
let subscription3 = query2.subscribeCallback!
224+
225+
//: As before, setup your subscription and event handlers.
226+
subscription3.handleSubscribe { subscribedQuery, isNew in
227+
228+
//: You can check this subscription is for this query.
229+
if isNew {
230+
print("Successfully subscribed to new query \(subscribedQuery)")
231+
} else {
232+
print("Successfully updated subscription to new query \(subscribedQuery)")
233+
}
234+
}
235+
236+
subscription3.handleEvent { _, event in
237+
switch event {
238+
239+
case .entered(let object):
240+
print("Entered: \(object)")
241+
case .left(let object):
242+
print("Left: \(object)")
243+
case .created(let object):
244+
print("Created: \(object)")
245+
case .updated(let object):
246+
print("Updated: \(object)")
247+
case .deleted(let object):
248+
print("Deleted: \(object)")
249+
}
250+
}
251+
252+
//: Now lets subscribe to an additional query.
253+
let subscription4 = query.subscribeCallback!
254+
255+
//: This is how you receive notifications about the success
256+
//: of your subscription.
257+
subscription4.handleSubscribe { subscribedQuery, isNew in
258+
259+
//: You can check this subscription is for this query
260+
if isNew {
261+
print("Successfully subscribed to new query \(subscribedQuery)")
262+
} else {
263+
print("Successfully updated subscription to new query \(subscribedQuery)")
264+
}
265+
}
266+
267+
//: This is how you register to receive notifications of events related to your LiveQuery.
268+
subscription4.handleEvent { _, event in
269+
switch event {
270+
271+
case .entered(let object):
272+
print("Entered: \(object)")
273+
case .left(let object):
274+
print("Left: \(object)")
275+
case .created(let object):
276+
print("Created: \(object)")
277+
case .updated(let object):
278+
print("Updated: \(object)")
279+
case .deleted(let object):
280+
print("Deleted: \(object)")
281+
}
282+
}
283+
284+
//: Now we will will unsubscribe from one of the subsriptions, but maintain the connection.
285+
subscription3.handleUnsubscribe { query in
207286
print("Unsubscribed from \(query)")
208287
}
209288

@@ -214,5 +293,14 @@ do {
214293
print(error)
215294
}
216295

296+
//: Ping the LiveQuery server
297+
ParseLiveQuery.client?.sendPing { error in
298+
if let error = error {
299+
print("Error pinging LiveQuery server: \(error)")
300+
} else {
301+
print("Successfully pinged server!")
302+
}
303+
}
304+
217305
PlaygroundPage.current.finishExecution()
218306
//: [Next](@next)

Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,7 @@ extension ParseLiveQuery {
238238
}
239239

240240
func removePendingSubscription(_ requestId: Int) {
241-
let requestIdToRemove = RequestId(value: requestId)
242241
self.pendingSubscriptions.removeAll(where: { $0.0.value == requestId })
243-
//Remove in subscriptions just in case the server
244-
//responded before this was called
245-
self.subscriptions.removeValue(forKey: requestIdToRemove)
246242
closeWebsocketIfNoSubscriptions()
247243
}
248244

@@ -422,8 +418,8 @@ extension ParseLiveQuery: LiveQuerySocketDelegate {
422418
} else {
423419
isNew = true
424420
}
425-
self.removePendingSubscription(subscribed.0.value)
426421
self.subscriptions[subscribed.0] = subscribed.1
422+
self.removePendingSubscription(subscribed.0.value)
427423
self.notificationQueue.async {
428424
subscribed.1.subscribeHandlerClosure?(isNew)
429425
}
@@ -433,6 +429,7 @@ extension ParseLiveQuery: LiveQuerySocketDelegate {
433429
guard let subscription = self.subscriptions[requestId] else {
434430
return
435431
}
432+
self.subscriptions.removeValue(forKey: requestId)
436433
self.removePendingSubscription(preliminaryMessage.requestId)
437434
self.notificationQueue.async {
438435
subscription.unsubscribeHandlerClosure?()
@@ -721,10 +718,6 @@ extension ParseLiveQuery {
721718
let updatedRecord = value
722719
updatedRecord.messageData = encoded
723720
self.send(record: updatedRecord, requestId: key) { _ in }
724-
} else {
725-
let error = ParseError(code: .unknownError,
726-
message: "ParseLiveQuery Error: Not subscribed to this query")
727-
throw error
728721
}
729722
}
730723
}

0 commit comments

Comments
 (0)