-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathContents.swift
128 lines (109 loc) · 3.99 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
//: [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
/*: start parse-server with
npm start -- --appId applicationId --clientKey clientKey --masterKey masterKey --mountPath /1
*/
/*: In Xcode, make sure you are building the "ParseSwift (macOS)" framework.
*/
initializeParseCustomObjectId()
//: 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(objectId: String, score: Int) {
self.objectId = objectId
self.score = score
}
init(objectId: String) {
self.objectId = objectId
}
}
//: Define initial GameScore this time with custom `objectId`.
//: customObjectId has to be enabled on the server for this to work.
var score = GameScore(objectId: "myObjectId", score: 10)
/*: Save asynchronously (preferred way) - Performs work on background
queue and returns to specified callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
score.save { result in
switch result {
case .success(let savedScore):
assert(savedScore.objectId != nil)
assert(savedScore.createdAt != nil)
assert(savedScore.updatedAt != nil)
assert(savedScore.ACL == nil)
assert(savedScore.score == 10)
//: Now that this object has a `createdAt`, it's properly saved to the server.
//: Any changes to `createdAt` and `objectId` will not be saved to the server.
print("Saved score: \(savedScore)")
/*: To modify, need to make it a var as the value type
was initialized as immutable. Using `emptyObject`
allows you to only send the updated keys to the
parse server as opposed to the whole object.
*/
var changedScore = savedScore.emptyObject
changedScore.score = 200
changedScore.save { result in
switch result {
case .success(let savedChangedScore):
assert(savedChangedScore.score == 200)
assert(savedScore.objectId == savedChangedScore.objectId)
print("Updated score: \(savedChangedScore)")
case .failure(let error):
assertionFailure("Error saving: \(error)")
}
}
case .failure(let error):
assertionFailure("Error saving: \(error)")
}
}
//: Fetch object
score.fetch { result in
switch result {
case .success(let fetchedScore):
print("Successfully fetched: \(fetchedScore)")
case .failure(let error):
assertionFailure("Error fetching: \(error)")
}
}
//: Query object
let query = GameScore.query("objectId" == "myObjectId")
query.first { result in
switch result {
case .success(let found):
print(found)
case .failure(let error):
print(error)
}
}
//: Now we will attempt to fetch a ParseObject that isn't saved.
let scoreToFetch = GameScore(objectId: "hello")
//: Asynchronously (preferred way) fetch this GameScore based on it's objectId alone.
scoreToFetch.fetch { result in
switch result {
case .success(let fetchedScore):
print("Successfully fetched: \(fetchedScore)")
case .failure(let error):
assertionFailure("Error fetching: \(error)")
}
}
PlaygroundPage.current.finishExecution()
//: [Next](@next)