Skip to content

Commit 5307210

Browse files
authored
style: improve playground examples (#339)
* fix: allow ParseRole to save when using custom objectId's * codecov * style: improve playground examples
1 parent 51e16d0 commit 5307210

File tree

14 files changed

+96
-69
lines changed

14 files changed

+96
-69
lines changed

ParseSwift.playground/Pages/1 - Your first Object.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct GameScore: ParseObject {
5353
}
5454

5555
//: It's recommended to place custom initializers in an extension
56-
//: to preserve the convenience initializer.
56+
//: to preserve the memberwise initializer.
5757
extension GameScore {
5858

5959
init(points: Int) {
@@ -95,7 +95,7 @@ struct GameData: ParseObject {
9595
}
9696

9797
//: It's recommended to place custom initializers in an extension
98-
//: to preserve the convenience initializer.
98+
//: to preserve the memberwise initializer.
9999
extension GameData {
100100

101101
init (bytes: ParseBytes?, polygon: ParsePolygon) {

ParseSwift.playground/Pages/10 - Cloud Code.xcplaygroundpage/Contents.swift

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,37 @@ struct Hello: ParseCloud {
2222
//: These are required by `ParseCloud`, you can set the default value to make it easier
2323
//: to use.
2424
var functionJobName: String = "hello"
25-
26-
//: If your cloud function takes arguments, they can be passed by creating properties:
27-
//var argument1: [String: Int] = ["test": 5]
2825
}
2926

3027
//: Create another `ParseCloud` type.
3128
struct TestCloudCode: ParseCloud {
3229

3330
//: Return type of your Cloud Function
34-
typealias ReturnType = String
31+
typealias ReturnType = [String: Int]
3532

3633
//: These are required by `ParseCloud`, you can set the default value to make it easier
3734
//: to use.
3835
var functionJobName: String = "testCloudCode"
3936

4037
//: If your cloud function takes arguments, they can be passed by creating properties:
41-
//var argument1: [String: Int] = ["test": 5]
38+
var argument1: [String: Int]
39+
}
40+
41+
//: Create another `ParseCloud` type.
42+
struct TestCloudCodeError: ParseCloud {
43+
44+
//: Return type of your Cloud Function
45+
typealias ReturnType = String
46+
47+
//: These are required by `ParseCloud`, you can set the default value to make it easier
48+
//: to use.
49+
var functionJobName: String = "testCloudCodeError"
4250
}
4351

4452
/*: Assuming you have the Cloud Function named "hello" on your parse-server:
4553
// main.js
46-
Parse.Cloud.define('hello', async () => {
54+
Parse.Cloud.define('hello', async (request) => {
55+
console.log('From client: ' + JSON.stringify(request));
4756
return 'Hello world!';
4857
});
4958
*/
@@ -61,13 +70,33 @@ hello.runFunction { result in
6170
/*: Assuming you have the Cloud Function named "testCloudCode" on your parse-server.
6271
You can catch custom errors created in Cloud Code:
6372
// main.js
64-
Parse.Cloud.define("testCloudCode", async() => {
65-
throw new Parse.Error(3000, "cloud has an error on purpose.");
73+
Parse.Cloud.define("testCloudCode", async(request) => {
74+
console.log('From client: ' + JSON.stringify(request));
75+
return request.params.argument1;
6676
});
6777
*/
68-
let testCloudCode = TestCloudCode()
78+
let testCloudCode = TestCloudCode(argument1: ["test": 5])
6979

7080
testCloudCode.runFunction { result in
81+
switch result {
82+
case .success(let response):
83+
print("Response from cloud function: \(response)")
84+
case .failure(let error):
85+
assertionFailure("Error: \(error.localizedDescription)")
86+
}
87+
}
88+
89+
/*: Assuming you have the Cloud Function named "testCloudCode" on your parse-server.
90+
You can catch custom errors created in Cloud Code:
91+
// main.js
92+
Parse.Cloud.define("testCloudCodeError", async(request) => {
93+
console.log('From client: ' + JSON.stringify(request));
94+
throw new Parse.Error(3000, "cloud has an error on purpose.");
95+
});
96+
*/
97+
let testCloudCodeError = TestCloudCodeError()
98+
99+
testCloudCodeError.runFunction { result in
71100
switch result {
72101
case .success:
73102
assertionFailure("Should have thrown a custom error")
@@ -98,7 +127,11 @@ testCloudCode.runFunction { result in
98127

99128
//: Jobs can be run the same way by using the method `startJob()`.
100129

101-
//: Saving objects with context for beforeSave, afterSave, etc.
130+
/*: Saving objects with context for beforeSave, afterSave, etc.
131+
Parse.Cloud.beforeSave("GameScore", async(request) => {
132+
console.log('From client context: ' + JSON.stringify(request.context));
133+
});
134+
*/
102135
//: Create your own value typed `ParseObject`.
103136
struct GameScore: ParseObject {
104137
//: These are required by ParseObject
@@ -123,7 +156,7 @@ struct GameScore: ParseObject {
123156
}
124157

125158
//: It's recommended to place custom initializers in an extension
126-
//: to preserve the convenience initializer.
159+
//: to preserve the memberwise initializer.
127160
extension GameScore {
128161
//: Custom initializer.
129162
init(points: Int) {

ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct GameScore: ParseObject {
4343
}
4444

4545
//: It's recommended to place custom initializers in an extension
46-
//: to preserve the convenience initializer.
46+
//: to preserve the memberwise initializer.
4747
extension GameScore {
4848
//: Custom initializer.
4949
init(name: String, points: Int) {

ParseSwift.playground/Pages/12 - Roles and Relations.xcplaygroundpage/Contents.swift

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct GameScore: ParseObject {
9090
}
9191

9292
//: It's recommended to place custom initializers in an extension
93-
//: to preserve the convenience initializer.
93+
//: to preserve the memberwise initializer.
9494
extension GameScore {
9595

9696
init(points: Int) {
@@ -108,32 +108,33 @@ extension GameScore {
108108
var savedRole: Role<User>?
109109

110110
//: Now we will create the Role.
111-
if let currentUser = User.current {
112-
113-
//: Every Role requires an ACL that can't be changed after saving.
114-
var acl = ParseACL()
115-
acl.setReadAccess(user: currentUser, value: true)
116-
acl.setWriteAccess(user: currentUser, value: true)
117-
118-
do {
119-
//: Create the actual Role with a name and ACL.
120-
let adminRole = try Role<User>(name: "Administrator", acl: acl)
121-
adminRole.save { result in
122-
switch result {
123-
case .success(let saved):
124-
print("The role saved successfully: \(saved)")
125-
print("Check your \"Role\" class in Parse Dashboard.")
126-
127-
//: Store the saved role so we can use it later...
128-
savedRole = saved
129-
130-
case .failure(let error):
131-
print("Error saving role: \(error)")
132-
}
111+
guard let currentUser = User.current else {
112+
fatalError("User currently isn't signed in")
113+
}
114+
115+
//: Every Role requires an ACL that can't be changed after saving.
116+
var acl = ParseACL()
117+
acl.setReadAccess(user: currentUser, value: true)
118+
acl.setWriteAccess(user: currentUser, value: true)
119+
120+
do {
121+
//: Create the actual Role with a name and ACL.
122+
let adminRole = try Role<User>(name: "Administrator", acl: acl)
123+
adminRole.save { result in
124+
switch result {
125+
case .success(let saved):
126+
print("The role saved successfully: \(saved)")
127+
print("Check your \"Role\" class in Parse Dashboard.")
128+
129+
//: Store the saved role so we can use it later...
130+
savedRole = saved
131+
132+
case .failure(let error):
133+
print("Error saving role: \(error)")
133134
}
134-
} catch {
135-
print("Error: \(error)")
136135
}
136+
} catch {
137+
print("Error: \(error)")
137138
}
138139

139140
//: Lets check to see if our Role has saved.
@@ -200,11 +201,6 @@ do {
200201
//: This variable will store the saved role.
201202
var savedRoleModerator: Role<User>?
202203

203-
//: We need another ACL.
204-
var acl = ParseACL()
205-
acl.setReadAccess(user: User.current!, value: true)
206-
acl.setWriteAccess(user: User.current!, value: false)
207-
208204
do {
209205
//: Create the actual Role with a name and ACL.
210206
let memberRole = try Role<User>(name: "Member", acl: acl)
@@ -352,27 +348,25 @@ do {
352348

353349
//: Now we will see how to use the stored `ParseRelation on` property in User to create query
354350
//: all of the relations to `scores`.
355-
var currentUser: User?
351+
var updatedCurrentUser: User
356352
do {
357353
//: Fetch the updated user since the previous relations were created on the server.
358-
currentUser = try User.current?.fetch()
359-
print("Updated current user with relation: \(String(describing: currentUser))")
354+
updatedCurrentUser = try User.current!.fetch()
355+
print("Updated current user with relation: \(updatedCurrentUser)")
360356
} catch {
357+
updatedCurrentUser = User.current!
361358
print("\(error.localizedDescription)")
362359
}
363360

364361
do {
365-
if let usableStoredRelation = try currentUser?.relation(currentUser?.scores, key: "scores") {
366-
try (usableStoredRelation.query() as Query<GameScore>).find { result in
367-
switch result {
368-
case .success(let scores):
369-
print("Found related scores from stored ParseRelation: \(scores)")
370-
case .failure(let error):
371-
print("Error finding scores from stored ParseRelation: \(error)")
372-
}
362+
let usableStoredRelation = try updatedCurrentUser.relation(updatedCurrentUser.scores, key: "scores")
363+
try (usableStoredRelation.query() as Query<GameScore>).find { result in
364+
switch result {
365+
case .success(let scores):
366+
print("Found related scores from stored ParseRelation: \(scores)")
367+
case .failure(let error):
368+
print("Error finding scores from stored ParseRelation: \(error)")
373369
}
374-
} else {
375-
print("Error: should unwrapped relation")
376370
}
377371
} catch {
378372
print("\(error.localizedDescription)")

ParseSwift.playground/Pages/13 - Operations.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct GameScore: ParseObject {
4141
}
4242

4343
//: It's recommended to place custom initializers in an extension
44-
//: to preserve the convenience initializer.
44+
//: to preserve the memberwise initializer.
4545
extension GameScore {
4646
//: Custom initializer.
4747
init(points: Int) {

ParseSwift.playground/Pages/15 - Custom ObjectId.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct GameScore: ParseObject {
4444
}
4545

4646
//: It's recommended to place custom initializers in an extension
47-
//: to preserve the convenience initializer.
47+
//: to preserve the memberwise initializer.
4848
extension GameScore {
4949
//: Custom initializer.
5050
init(objectId: String, points: Int) {

ParseSwift.playground/Pages/17 - SwiftUI - Finding Objects.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct GameScore: ParseObject {
5757
}
5858

5959
//: It's recommended to place custom initializers in an extension
60-
//: to preserve the convenience initializer.
60+
//: to preserve the memberwise initializer.
6161
extension GameScore {
6262
//: Custom initializer.
6363
init(name: String, points: Int) {

ParseSwift.playground/Pages/18 - SwiftUI - Finding Objects With Custom ViewModel.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct GameScore: ParseObject {
5353
}
5454

5555
//: It's recommended to place custom initializers in an extension
56-
//: to preserve the convenience initializer.
56+
//: to preserve the memberwise initializer.
5757
extension GameScore {
5858
//: Custom initializer.
5959
init(name: String, points: Int) {

ParseSwift.playground/Pages/19 - SwiftUI - LiveQuery.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct GameScore: ParseObject {
5151
}
5252

5353
//: It's recommended to place custom initializers in an extension
54-
//: to preserve the convenience initializer.
54+
//: to preserve the memberwise initializer.
5555
extension GameScore {
5656
//: Custom initializer.
5757
init(name: String, points: Int) {

ParseSwift.playground/Pages/4 - User - Continued.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct User: ParseUser {
5858
}
5959

6060
//: It's recommended to place custom initializers in an extension
61-
//: to preserve the convenience initializer.
61+
//: to preserve the memberwise initializer.
6262
extension User {
6363
//: Custom init for signup.
6464
init(username: String, password: String, email: String) {
@@ -92,7 +92,7 @@ struct GameScore: ParseObject {
9292
}
9393

9494
//: It's recommended to place custom initializers in an extension
95-
//: to preserve the convenience initializer.
95+
//: to preserve the memberwise initializer.
9696
extension GameScore {
9797
//: Custom initializer.
9898
init(points: Int) {

0 commit comments

Comments
 (0)