-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Contents.swift
228 lines (196 loc) · 6.62 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
//: [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 User: ParseUser {
//: These are required for `ParseObject`.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: These are required for `ParseUser`.
var username: String?
var email: String?
var emailVerified: Bool?
var password: String?
var authData: [String: [String: String]?]?
//: Your custom keys.
var customKey: String?
var score: GameScore?
var targetScore: GameScore?
var allScores: [GameScore]?
}
//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
extension User {
//: Custom init for signup.
init(username: String, password: String, email: String) {
self.username = username
self.password = password
self.email = email
}
}
//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
//: Those are required for Object
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: Your own properties.
var score: Int? = 0
}
//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
extension GameScore {
//: Custom initializer.
init(score: Int) {
self.score = score
}
init(objectId: String?) {
self.objectId = objectId
}
}
//: Logging out - synchronously
do {
try User.logout()
print("Successfully logged out")
} catch let error {
print("Error logging out: \(error)")
}
/*: Login - asynchronously - Performs work on background
queue and returns to specified callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
User.login(username: "hello", password: "world") { result in
switch result {
case .success(let user):
guard let currentUser = User.current else {
assertionFailure("Error: current user not stored locally")
return
}
assert(currentUser.hasSameObjectId(as: user))
print("Successfully logged in as user: \(user)")
case .failure(let error):
print("Error logging in: \(error)")
}
}
/*: Save your first `customKey` value to your `ParseUser`
Asynchrounously - Performs work on background
queue and returns to specified callbackQueue.
If no callbackQueue is specified it returns to main queue.
Using `emptyObject` allows you to only send the updated keys to the
parse server as opposed to the whole object.
*/
var currentUser = User.current?.emptyObject
currentUser?.customKey = "myCustom"
currentUser?.score = GameScore(score: 12)
currentUser?.targetScore = GameScore(score: 100)
currentUser?.allScores = [GameScore(score: 5), GameScore(score: 8)]
currentUser?.save { result in
switch result {
case .success(let updatedUser):
print("Successfully save custom fields of User to ParseServer: \(updatedUser)")
case .failure(let error):
print("Failed to update user: \(error)")
}
}
//: Looking at the output of user from the previous login, it only has
//: a pointer to the `score` and `targetScore` fields. You can
//: fetch using `include` to get the score.
User.current?.fetch(includeKeys: ["score"]) { result in
switch result {
case .success:
print("Successfully fetched user with score key: \(String(describing: User.current))")
case .failure(let error):
print("Error fetching score: \(error)")
}
}
//: The `target` score is still missing. You can get all pointer fields at
//: once by including `["*"]`.
User.current?.fetch(includeKeys: ["*"]) { result in
switch result {
case .success:
print("Successfully fetched user with all keys: \(String(describing: User.current))")
case .failure(let error):
print("Error fetching score: \(error)")
}
}
//: Logging out - synchronously.
do {
try User.logout()
print("Successfully logged out")
} catch let error {
print("Error logging out: \(error)")
}
//: To add additional information when signing up a user,
//: you should create an instance of your user first.
var newUser = User(username: "parse", password: "aPassword*", email: "parse@parse.com")
//: Add any other additional information.
newUser.customKey = "mind"
newUser.signup { result in
switch result {
case .success(let user):
guard let currentUser = User.current else {
assertionFailure("Error: current user not stored locally")
return
}
assert(currentUser.hasSameObjectId(as: user))
print("Successfully signed up as user: \(user)")
case .failure(let error):
print("Error logging in: \(error)")
}
}
//: Logging out - synchronously.
do {
try User.logout()
print("Successfully logged out")
} catch let error {
print("Error logging out: \(error)")
}
//: Verification Email - synchronously.
do {
try User.verificationEmail(email: "hello@parse.org")
print("Successfully requested verification email be sent")
} catch let error {
print("Error requesting verification email be sent: \(error)")
}
//: Password Reset Request - synchronously.
do {
try User.passwordReset(email: "hello@parse.org")
print("Successfully requested password reset")
} catch let error {
print("Error requesting password reset: \(error)")
}
//: Logging in anonymously.
User.anonymous.login { result in
switch result {
case .success:
print("Successfully logged in \(String(describing: User.current))")
print("Session token: \(String(describing: User.current?.sessionToken))")
case .failure(let error):
print("Error logging in: \(error)")
}
}
//: Convert the anonymous user to a real new user.
var currentUser2 = User.current?.emptyObject
currentUser2?.username = "bye"
currentUser2?.password = "world"
currentUser2?.signup { result in
switch result {
case .success(let user):
print("Parse signup successful: \(user)")
print("Session token: \(String(describing: User.current?.sessionToken))")
case .failure(let error):
print("Error logging in: \(error)")
}
}
PlaygroundPage.current.finishExecution()
//: [Next](@next)