@@ -43,7 +43,7 @@ struct GameScore: ParseObject {
43
43
//: Be sure you have LiveQuery enabled on your server.
44
44
45
45
//: Create a query just as you normally would.
46
- var query = GameScore . query ( " score " > 9 )
46
+ var query = GameScore . query ( " score " < 11 )
47
47
48
48
#if canImport(SwiftUI)
49
49
//: To use subscriptions inside of SwiftUI
@@ -61,7 +61,7 @@ struct ContentView: View {
61
61
Text ( " Unsubscribed from query! " )
62
62
} else if let event = subscription. event {
63
63
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.
65
65
switch event. event {
66
66
67
67
case . entered( let object) :
@@ -116,7 +116,7 @@ subscription.handleSubscribe { subscribedQuery, isNew in
116
116
}
117
117
}
118
118
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.
120
120
subscription. handleEvent { _, event in
121
121
switch event {
122
122
@@ -133,10 +133,19 @@ subscription.handleEvent { _, event in
133
133
}
134
134
}
135
135
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
+
136
145
//: Now go to your dashboard, go to the GameScore table and add, update or remove rows.
137
146
//: You should receive notifications for each.
138
147
139
- //: This is how you register to receive notificaitons about being unsubscribed.
148
+ //: This is how you register to receive notifications about being unsubscribed.
140
149
subscription. handleUnsubscribe { query in
141
150
print ( " Unsubscribed from \( query) " )
142
151
}
148
157
print ( error)
149
158
}
150
159
151
- //: Ping the LiveQuery server
160
+ //: Ping the LiveQuery server. This should produce an error
161
+ //: because LiveQuery is disconnected.
152
162
ParseLiveQuery . client? . sendPing { error in
153
163
if let error = error {
154
164
print ( " Error pinging LiveQuery server: \( error) " )
@@ -157,12 +167,6 @@ ParseLiveQuery.client?.sendPing { error in
157
167
}
158
168
}
159
169
160
- //: To close the current LiveQuery connection.
161
- ParseLiveQuery . client? . close ( )
162
-
163
- //: To close all LiveQuery connections.
164
- ParseLiveQuery . client? . closeAll ( )
165
-
166
170
//: Create a new query.
167
171
var query2 = GameScore . query ( " score " > 50 )
168
172
@@ -199,11 +203,86 @@ subscription2.handleEvent { _, event in
199
203
}
200
204
}
201
205
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()
204
211
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
207
286
print ( " Unsubscribed from \( query) " )
208
287
}
209
288
@@ -214,5 +293,14 @@ do {
214
293
print ( error)
215
294
}
216
295
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
+
217
305
PlaygroundPage . current. finishExecution ( )
218
306
//: [Next](@next)
0 commit comments