-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathContents.swift
196 lines (159 loc) · 5.76 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
//: [Previous](@previous)
//: For this page, make sure your build target is set to ParseSwift (iOS) and targeting
//: an iPhone, iPod, or iPad. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = iOS`. This is because
//: SwiftUI in macOS Playgrounds doesn't seem to build correctly
//: Be sure to switch your target and `Playground Settings` back to
//: macOS after leaving this page.
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?
//: Custom initializer.
init(name: String, score: Int) {
self.name = name
self.score = score
}
}
//: Be sure you have LiveQuery enabled on your server.
//: Create a query just as you normally would.
var query = GameScore.query("score" > 9)
//: To use subscriptions inside of SwiftUI
struct ContentView: View {
//: A LiveQuery subscription can be used as a view model in SwiftUI
@ObservedObject var subscription = query.subscribe!
var body: some View {
VStack {
if subscription.subscribed != nil {
Text("Subscribed to query!")
} else if subscription.unsubscribed != nil {
Text("Unsubscribed from query!")
} else if let event = subscription.event {
//: This is how you register to receive notificaitons of events related to your LiveQuery.
switch event.event {
case .entered(let object):
Text("Entered with score: \(object.score)")
case .left(let object):
Text("Left with score: \(object.score)")
case .created(let object):
Text("Created with score: \(object.score)")
case .updated(let object):
Text("Updated with score: \(object.score)")
case .deleted(let object):
Text("Deleted with score: \(object.score)")
}
} else {
Text("Not subscribed to a query")
}
Spacer()
Text("Update GameScore in Parse Dashboard to see changes here")
Button(action: {
try? query.unsubscribe()
}, label: {
Text("Unsubscribe")
.font(.headline)
.background(Color.red)
.foregroundColor(.white)
.padding()
.cornerRadius(20.0)
.frame(width: 300, height: 50)
})
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
//: 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 notificaitons 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)")
}
}
//: 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 notificaitons about being unsubscribed.
subscription.handleUnsubscribe { query in
print("Unsubscribed from \(query)")
}
//: To unsubscribe from your query.
do {
try query.unsubscribe()
} catch {
print(error)
}
//: 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 and event 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)")
}
}
//: Now go to your dashboard, go to the GameScore table and add, update or remove rows.
//: You should receive notifications for each, but only with your fields information.
//: This is how you register to receive notificaitons about being unsubscribed.
subscription2.handleUnsubscribe { query in
print("Unsubscribed from \(query)")
}
//: To unsubscribe from your query.
do {
try query2.unsubscribe()
} catch {
print(error)
}
PlaygroundPage.current.finishExecution()
//: [Next](@next)