-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Contents.swift
143 lines (117 loc) · 4.03 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
//: [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()
//: Create your own value typed `ParseObject`.
struct Book: ParseObject {
//: Those are required for Object
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: Your own properties.
var title: String?
init(title: String) {
self.title = title
}
}
struct Author: ParseObject {
//: Those are required for Object.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: Your own properties.
var name: String
var book: Book
var otherBooks: [Book]?
init(name: String, book: Book) {
self.name = name
self.book = book
}
}
let newBook = Book(title: "hello")
let author = Author(name: "Alice", book: newBook)
author.save { result in
switch result {
case .success(let savedAuthorAndBook):
assert(savedAuthorAndBook.objectId != nil)
assert(savedAuthorAndBook.createdAt != nil)
assert(savedAuthorAndBook.updatedAt != nil)
assert(savedAuthorAndBook.ACL == nil)
print("Saved \(savedAuthorAndBook)")
case .failure(let error):
assertionFailure("Error saving: \(error)")
}
}
//: Pointer array.
let otherBook1 = Book(title: "I like this book")
let otherBook2 = Book(title: "I like this book also")
var author2 = Author(name: "Bruce", book: newBook)
author2.otherBooks = [otherBook1, otherBook2]
author2.save { result in
switch result {
case .success(let savedAuthorAndBook):
assert(savedAuthorAndBook.objectId != nil)
assert(savedAuthorAndBook.createdAt != nil)
assert(savedAuthorAndBook.updatedAt != nil)
assert(savedAuthorAndBook.ACL == nil)
assert(savedAuthorAndBook.otherBooks?.count == 2)
print("Saved \(savedAuthorAndBook)")
case .failure(let error):
assertionFailure("Error saving: \(error)")
}
}
//: Query for your new saved author
let query1 = Author.query("name" == "Bruce")
query1.first { results in
switch results {
case .success(let author):
print("Found author: \(author)")
case .failure(let error):
assertionFailure("Error querying: \(error)")
}
}
/*: You will notice in the query above, the fields `book` and `otherBooks` only contain
arrays consisting of key/value pairs of `objectId`. These are called Pointers
in `Parse`.
If you want to retrieve the complete object pointed to in `book`, you need to add
the field names containing the objects specifically in `include` in your query.
*/
/*: Here, we include `book`. If you wanted `book` and `otherBook`, you
could have used: `.include(["book", "otherBook"])`.
*/
let query2 = Author.query("name" == "Bruce")
.include("book")
query2.first { results in
switch results {
case .success(let author):
print("Found author and included \"book\": \(author)")
case .failure(let error):
assertionFailure("Error querying: \(error)")
}
}
/*: When you have many fields that are pointing to objects, it may become tedious
to add all of them to the list. You can quickly retreive all pointer objects by
using `includeAll`. You can also use `include("*")` to retrieve all pointer
objects.
*/
let query3 = Author.query("name" == "Bruce")
.includeAll()
query3.first { results in
switch results {
case .success(let author):
print("Found author and included all: \(author)")
case .failure(let error):
assertionFailure("Error querying: \(error)")
}
}
PlaygroundPage.current.finishExecution()
//: [Next](@next)