-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Contents.swift
194 lines (167 loc) · 6.23 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
//: [Previous](@previous)
//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = macOS`. This is because
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
//: be set to build for `macOS` unless specified.
import PlaygroundSupport
import Foundation
import ParseSwift
PlaygroundPage.current.needsIndefiniteExecution = true
initializeParse()
struct GameScore: ParseObject {
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var originalData: Data?
//: Your own properties.
var points: Int?
var timeStamp: Date? = Date()
var oldScore: Int?
var isHighest: Bool?
/*:
Optional - implement your own version of merge
for faster decoding after updating your `ParseObject`.
*/
func merge(with object: Self) throws -> Self {
var updated = try mergeParse(with: object)
if updated.shouldRestoreKey(\.points,
original: object) {
updated.points = object.points
}
if updated.shouldRestoreKey(\.timeStamp,
original: object) {
updated.timeStamp = object.timeStamp
}
if updated.shouldRestoreKey(\.oldScore,
original: object) {
updated.oldScore = object.oldScore
}
if updated.shouldRestoreKey(\.isHighest,
original: object) {
updated.isHighest = object.isHighest
}
return updated
}
}
var score = GameScore()
score.points = 200
score.oldScore = 10
score.isHighest = true
do {
try score.save()
} catch {
print(error)
}
let afterDate = Date().addingTimeInterval(-300)
var query = GameScore.query("points" > 50,
"createdAt" > afterDate)
.order([.descending("points")])
//: Query asynchronously (preferred way) - Performs work on background
//: queue and returns to specified callbackQueue.
//: If no callbackQueue is specified it returns to main queue.
query.limit(2)
.order([.descending("points")])
.find(callbackQueue: .main) { results in
switch results {
case .success(let scores):
assert(scores.count >= 1)
scores.forEach { score in
guard let createdAt = score.createdAt else { fatalError() }
assert(createdAt.timeIntervalSince1970 > afterDate.timeIntervalSince1970, "date should be ok")
print("Found score: \(score)")
}
case .failure(let error):
if error.equalsTo(.objectNotFound) {
assertionFailure("Object not found for this query")
} else {
assertionFailure("Error querying: \(error)")
}
}
}
//: Query synchronously (not preferred - all operations on current queue).
let results = try query.find()
assert(results.count >= 1)
results.forEach { score in
guard let createdAt = score.createdAt else { fatalError() }
assert(createdAt.timeIntervalSince1970 > afterDate.timeIntervalSince1970, "date should be ok")
print("Found score: \(score)")
}
//: Query first asynchronously (preferred way) - Performs work on background
//: queue and returns to specified callbackQueue.
//: If no callbackQueue is specified it returns to main queue.
query.first { results in
switch results {
case .success(let score):
guard score.objectId != nil,
let createdAt = score.createdAt else { fatalError() }
assert(createdAt.timeIntervalSince1970 > afterDate.timeIntervalSince1970, "date should be ok")
print("Found score: \(score)")
case .failure(let error):
if error.containedIn([.objectNotFound, .invalidQuery]) {
assertionFailure("The query is invalid or the object is not found.")
} else {
assertionFailure("Error querying: \(error)")
}
}
}
//: Query first asynchronously (preferred way) - Performs work on background
//: queue and returns to specified callbackQueue.
//: If no callbackQueue is specified it returns to main queue.
query.withCount { results in
switch results {
case .success(let (score, count)):
print("Found scores: \(score) total amount: \(count)")
case .failure(let error):
if error.containedIn([.objectNotFound, .invalidQuery]) {
assertionFailure("The query is invalid or the object is not found.")
} else {
assertionFailure("Error querying: \(error)")
}
}
}
//: Query based on relative time.
let queryRelative = GameScore.query(relative("createdAt" < "in 10 minutes"))
queryRelative.find { results in
switch results {
case .success(let scores):
print("Found scores using relative time: \(scores)")
case .failure(let error):
print("Error querying: \(error)")
}
}
let querySelect = query.select("points")
querySelect.first { results in
switch results {
case .success(let score):
guard score.objectId != nil,
let createdAt = score.createdAt else { fatalError() }
assert(createdAt.timeIntervalSince1970 > afterDate.timeIntervalSince1970, "date should be ok")
print("Found score using select: \(score)")
case .failure(let error):
if let parseError = error.equalsTo(.objectNotFound) {
assertionFailure("Object not found: \(parseError)")
} else {
assertionFailure("Error querying: \(error)")
}
}
}
let queryExclude = query.exclude("points")
queryExclude.first { results in
switch results {
case .success(let score):
guard score.objectId != nil,
let createdAt = score.createdAt else { fatalError() }
assert(createdAt.timeIntervalSince1970 > afterDate.timeIntervalSince1970, "date should be ok")
print("Found score using exclude: \(score)")
case .failure(let error):
if let parseError = error.containedIn(.objectNotFound, .invalidQuery) {
assertionFailure("Matching error found: \(parseError)")
} else {
assertionFailure("Error querying: \(error)")
}
}
}
PlaygroundPage.current.finishExecution()
//: [Next](@next)