Skip to content

Commit 50935e0

Browse files
authored
Add Playground example of custom objectId (#137)
* Add Playground example of custom objectId * Add changelog and more docs * nit
1 parent a7a6f40 commit 50935e0

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.7.1...main)
55
* _Contributing to this repo? Add info about your change here to be included in the next release_
66

7+
__Improvements__
8+
- Add playground example of saving Parse objects with a custom objectId ([#137](https://github.com/parse-community/Parse-Swift/pull/137)), thanks to [Corey Baker](https://github.com/cbaker6).
9+
710
### 1.7.1
811
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.7.0...1.7.1)
912

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//: [Previous](@previous)
2+
3+
//: For this page, make sure your build target is set to ParseSwift (macOS) and targeting
4+
//: `My Mac` or whatever the name of your mac is. Also be sure your `Playground Settings`
5+
//: in the `File Inspector` is `Platform = macOS`. This is because
6+
//: Keychain in iOS Playgrounds behaves differently. Every page in Playgrounds should
7+
//: be set to build for `macOS` unless specified.
8+
9+
import PlaygroundSupport
10+
import Foundation
11+
import ParseSwift
12+
PlaygroundPage.current.needsIndefiniteExecution = true
13+
14+
/*: start parse-server with
15+
npm start -- --appId applicationId --clientKey clientKey --masterKey masterKey --mountPath /1
16+
*/
17+
18+
/*: In Xcode, make sure you are building the "ParseSwift (macOS)" framework.
19+
*/
20+
21+
initializeParseCustomObjectId()
22+
23+
//: Create your own value typed `ParseObject`.
24+
struct GameScore: ParseObject {
25+
//: Those are required for Object
26+
var objectId: String?
27+
var createdAt: Date?
28+
var updatedAt: Date?
29+
var ACL: ParseACL?
30+
31+
//: Your own properties.
32+
var score: Int = 0
33+
34+
//: Custom initializer.
35+
init(objectId: String, score: Int) {
36+
self.objectId = objectId
37+
self.score = score
38+
}
39+
}
40+
41+
//: Define initial GameScore this time with custom `objectId`.
42+
//: customObjectId has to be enabled on the server for this to work.
43+
var score = GameScore(objectId: "myObjectId", score: 10)
44+
45+
/*: Save asynchronously (preferred way) - Performs work on background
46+
queue and returns to specified callbackQueue.
47+
If no callbackQueue is specified it returns to main queue.
48+
*/
49+
score.save { result in
50+
switch result {
51+
case .success(let savedScore):
52+
assert(savedScore.objectId != nil)
53+
assert(savedScore.createdAt != nil)
54+
assert(savedScore.updatedAt != nil)
55+
assert(savedScore.ACL == nil)
56+
assert(savedScore.score == 10)
57+
58+
//: Now that this object has a `createdAt`, it's properly saved to the server.
59+
//: Any changes to `createdAt` and `objectId` will not be saved to the server.
60+
print("Saved score: \(savedScore)")
61+
62+
/*: To modify, need to make it a var as the value type
63+
was initialized as immutable.
64+
*/
65+
var changedScore = savedScore
66+
changedScore.score = 200
67+
changedScore.save { result in
68+
switch result {
69+
case .success(var savedChangedScore):
70+
assert(savedChangedScore.score == 200)
71+
assert(savedScore.objectId == savedChangedScore.objectId)
72+
print("Updated score: \(savedChangedScore)")
73+
74+
case .failure(let error):
75+
assertionFailure("Error saving: \(error)")
76+
}
77+
}
78+
case .failure(let error):
79+
assertionFailure("Error saving: \(error)")
80+
}
81+
}
82+
83+
//: [Next](@next)

ParseSwift.playground/Sources/Common.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ public func initializeParse() {
77
masterKey: "masterKey",
88
serverURL: URL(string: "http://localhost:1337/1")!)
99
}
10+
11+
public func initializeParseCustomObjectId() {
12+
ParseSwift.initialize(applicationId: "applicationId",
13+
clientKey: "clientKey",
14+
serverURL: URL(string: "http://localhost:1337/1")!,
15+
allowCustomObjectId: true)
16+
}

ParseSwift.playground/contents.xcplayground

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
<page name='12 - Roles and Relations'/>
1616
<page name='13 - Operations'/>
1717
<page name='14 - Config'/>
18+
<page name='15 - Custom ObjectId'/>
1819
</pages>
1920
</playground>

0 commit comments

Comments
 (0)