|
| 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) |
0 commit comments