-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathContents.swift
252 lines (206 loc) · 6.77 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//: [Previous](@previous)
import PlaygroundSupport
import Foundation
import ParseSwift
import SwiftUI
PlaygroundPage.current.needsIndefiniteExecution = true
initializeParse()
//: Create your own value typed ParseObject.
struct GameScore: ParseObject {
//: These are required for any Object.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: Your own properties.
var score: Int = 0
var location: ParseGeoPoint?
var name: String?
}
//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
extension GameScore {
//: Custom initializer.
init(name: String, score: Int) {
self.name = name
self.score = score
}
}
//: Create a delegate for LiveQuery errors
class LiveQueryDelegate: ParseLiveQueryDelegate {
func received(_ error: Error) {
print(error)
}
func closedSocket(_ code: URLSessionWebSocketTask.CloseCode?, reason: Data?) {
print("Socket closed with \(String(describing: code)) and \(String(describing: reason))")
}
}
//: Be sure you have LiveQuery enabled on your server.
//: Set the delegate.
let delegate = LiveQueryDelegate()
if let socket = ParseLiveQuery.getDefault() {
socket.receiveDelegate = delegate
}
//: Create a query just as you normally would.
var query = GameScore.query("score" < 11)
//: This is how you subscribe to your created query using callbacks.
let subscription = query.subscribeCallback!
//: This is how you receive notifications about the success
//: of your subscription.
subscription.handleSubscribe { subscribedQuery, isNew in
//: You can check this subscription is for this query
if isNew {
print("Successfully subscribed to new query \(subscribedQuery)")
} else {
print("Successfully updated subscription to new query \(subscribedQuery)")
}
}
//: This is how you register to receive notifications of events related to your LiveQuery.
subscription.handleEvent { _, event in
switch event {
case .entered(let object):
print("Entered: \(object)")
case .left(let object):
print("Left: \(object)")
case .created(let object):
print("Created: \(object)")
case .updated(let object):
print("Updated: \(object)")
case .deleted(let object):
print("Deleted: \(object)")
}
}
//: Ping the LiveQuery server
ParseLiveQuery.client?.sendPing { error in
if let error = error {
print("Error pinging LiveQuery server: \(error)")
} else {
print("Successfully pinged server!")
}
}
//: Now go to your dashboard, go to the GameScore table and add, update or remove rows.
//: You should receive notifications for each.
//: This is how you register to receive notifications about being unsubscribed.
subscription.handleUnsubscribe { query in
print("Unsubscribed from \(query)")
}
//: To unsubscribe from your query.
do {
try query.unsubscribe()
} catch {
print(error)
}
//: If you look at your server log, you will notice the client and server disconnnected.
//: This is because there is no more LiveQuery subscriptions.
//: Ping the LiveQuery server. This should produce an error
//: because LiveQuery is disconnected.
ParseLiveQuery.client?.sendPing { error in
if let error = error {
print("Error pinging LiveQuery server: \(error)")
} else {
print("Successfully pinged server!")
}
}
//: Create a new query.
var query2 = GameScore.query("score" > 50)
//: Select the fields you are interested in receiving.
query2.fields("score")
//: Subscribe to your new query.
let subscription2 = query2.subscribeCallback!
//: As before, setup your subscription, event, and unsubscribe handlers.
subscription2.handleSubscribe { subscribedQuery, isNew in
//: You can check this subscription is for this query.
if isNew {
print("Successfully subscribed to new query \(subscribedQuery)")
} else {
print("Successfully updated subscription to new query \(subscribedQuery)")
}
}
subscription2.handleEvent { _, event in
switch event {
case .entered(let object):
print("Entered: \(object)")
case .left(let object):
print("Left: \(object)")
case .created(let object):
print("Created: \(object)")
case .updated(let object):
print("Updated: \(object)")
case .deleted(let object):
print("Deleted: \(object)")
}
}
subscription2.handleUnsubscribe { query in
print("Unsubscribed from \(query)")
}
//: To close the current LiveQuery connection.
ParseLiveQuery.client?.close()
//: To close all LiveQuery connections use:
//ParseLiveQuery.client?.closeAll()
//: Ping the LiveQuery server. This should produce an error
//: because LiveQuery is disconnected.
ParseLiveQuery.client?.sendPing { error in
if let error = error {
print("Error pinging LiveQuery server: \(error)")
} else {
print("Successfully pinged server!")
}
}
//: Resubscribe to your previous query.
//: Since we never unsubscribed you can use your previous handlers.
let subscription3 = query2.subscribeCallback!
//: Resubscribe to another previous query.
//: This one needs new handlers.
let subscription4 = query.subscribeCallback!
//: Need a new handler because we previously unsubscribed.
subscription4.handleSubscribe { subscribedQuery, isNew in
//: You can check this subscription is for this query
if isNew {
print("Successfully subscribed to new query \(subscribedQuery)")
} else {
print("Successfully updated subscription to new query \(subscribedQuery)")
}
}
//: Need a new event handler because we previously unsubscribed.
subscription4.handleEvent { _, event in
switch event {
case .entered(let object):
print("Entered: \(object)")
case .left(let object):
print("Left: \(object)")
case .created(let object):
print("Created: \(object)")
case .updated(let object):
print("Updated: \(object)")
case .deleted(let object):
print("Deleted: \(object)")
}
}
//: Need a new unsubscribe handler because we previously unsubscribed.
subscription4.handleUnsubscribe { query in
print("Unsubscribed from \(query)")
}
//: To unsubscribe from your query.
do {
try query2.unsubscribe()
} catch {
print(error)
}
//: Ping the LiveQuery server
ParseLiveQuery.client?.sendPing { error in
if let error = error {
print("Error pinging LiveQuery server: \(error)")
} else {
print("Successfully pinged server!")
}
}
//: To unsubscribe from your your last query.
do {
try query.unsubscribe()
} catch {
print(error)
}
//: If you look at your server log, you will notice the client and server disconnnected.
//: This is because there is no more LiveQuery subscriptions.
PlaygroundPage.current.finishExecution()
//: [Next](@next)