Skip to content

LiveQuery SwiftUI example in playgrounds #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import PlaygroundSupport
import Foundation
import ParseSwift
import SwiftUI
PlaygroundPage.current.needsIndefiniteExecution = true

initializeParse()
Expand Down Expand Up @@ -32,9 +33,61 @@ struct GameScore: ParseObject {
//: Create a query just as you normally would.
var query = GameScore.query("score" > 9)

//: 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 notificaitons 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")
}

Spacer()

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)
.frame(width: 300, height: 50)
})
}
}
}

PlaygroundPage.current.setLiveView(ContentView())

//: This is how you subscribe to your created query using callbacks.
//: Note that if you want to use subscriptions with SwiftUI, you should
//: use `let subscription = query.subscribe` instead.
let subscription = query.subscribeCallback!

//: This is how you receive notifications about the success
Expand Down
2 changes: 1 addition & 1 deletion ParseSwift.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='macos' display-mode='raw' buildActiveScheme='true' timelineScrubberEnabled='true' last-migration='1130'>
<playground version='6.0' target-platform='ios' display-mode='raw' buildActiveScheme='true' timelineScrubberEnabled='true' last-migration='1130'>
<pages>
<page name='1 - Your first Object'/>
<page name='2 - Finding Objects'/>
Expand Down