diff --git a/ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift b/ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift index 47caea756..f8820a8e7 100644 --- a/ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift +++ b/ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift @@ -3,6 +3,7 @@ import PlaygroundSupport import Foundation import ParseSwift +import SwiftUI PlaygroundPage.current.needsIndefiniteExecution = true initializeParse() @@ -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 diff --git a/ParseSwift.playground/contents.xcplayground b/ParseSwift.playground/contents.xcplayground index ab9557970..b482d7b97 100644 --- a/ParseSwift.playground/contents.xcplayground +++ b/ParseSwift.playground/contents.xcplayground @@ -1,5 +1,5 @@ - +