-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathContents.swift
102 lines (83 loc) · 3.14 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
//: [Previous](@previous)
//: If you are using Xcode 13+, ignore the comments below:
//: For this page, make sure your build target is set to ParseSwift (iOS) and targeting
//: an iPhone, iPod, or iPad. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = iOS`. This is because
//: SwiftUI in macOS Playgrounds doesn't seem to build correctly
//: Be sure to switch your target and `Playground Settings` back to
//: macOS after leaving this page.
import PlaygroundSupport
import Foundation
import ParseSwift
import SwiftUI
PlaygroundPage.current.needsIndefiniteExecution = true
initializeParse()
//: Create your own value typed ParseObject.
struct GameScore: ParseObject {
//: These are required for any Object.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: Your own properties.
var score: Int = 0
var location: ParseGeoPoint?
var name: String?
}
//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
extension GameScore {
//: Custom initializer.
init(name: String, score: Int) {
self.name = name
self.score = score
}
}
//: Be sure you have LiveQuery enabled on your server.
//: Create a query just as you normally would.
var query = GameScore.query("score" < 11)
//: To use subscriptions inside of SwiftUI
struct ContentView: View {
//: A LiveQuery subscription can be used as a view model in SwiftUI
@ObservedObject var subscription = query.subscribe!
var body: some View {
VStack {
if subscription.subscribed != nil {
Text("Subscribed to query!")
} else if subscription.unsubscribed != nil {
Text("Unsubscribed from query!")
} else if let event = subscription.event {
//: This is how you register to receive notifications of events related to your LiveQuery.
switch event.event {
case .entered(let object):
Text("Entered with score: \(object.score)")
case .left(let object):
Text("Left with score: \(object.score)")
case .created(let object):
Text("Created with score: \(object.score)")
case .updated(let object):
Text("Updated with score: \(object.score)")
case .deleted(let object):
Text("Deleted with score: \(object.score)")
}
} else {
Text("Not subscribed to a query")
}
Text("Update GameScore in Parse Dashboard to see changes here:")
Button(action: {
try? query.unsubscribe()
}, label: {
Text("Unsubscribe")
.font(.headline)
.background(Color.red)
.foregroundColor(.white)
.padding()
.cornerRadius(20.0)
})
Spacer()
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
PlaygroundPage.current.finishExecution()
//: [Next](@next)