Parse Cloud - LiveQueries - iOS Client doesn't work
The problem with this code is that you are placing this code var subscription: Subscription<PFObject>?
inside the function.
This object must be able to retain it's memory address in order for it to receive events.
For example.
class SomeClass {
var objects: [PFObject] = []
var subscription: Subscription<PFObject>?
var subscriber: ParseLiveQuery.Client!
let query = PFQuery(className: "Locations")
func startListener() {
// initialize the client
subscriber = ParseLiveQuery.Client()
// initialize subscriber and start subscription
subscription = subscriber.subscribe(conversationQuery)
// handle the event listenrs.
_ = subscription?.handleEvent({ (_, event) in
switch event {
case .created(let object):
self.objects.append(object)
// do stuff
default:
break // do other stuff or do nothing
}
})
}
}
As you can see from this code, I've placed the variables outside the function definition in order for the Subscription
's memory address to be retained.
Parse Livequery changes not printing when there's an update
The solution to fixing this issue was related to where everything was defined. Refer to the awnser to this stackoverflow question: Parse Cloud - LiveQueries - iOS Client doesn't work There's sufficient explanation there too.
Parse LiveQuery in Cloud Code
Live Query is not made to use on the server side.
Live Query use web sockets, to interact in real time with your client.
But when you app is close the socket is close too ... So want you need is push notifications
If you want to make a real time messaging application with parse your need to:
- Implement Parse Live Query to get new messages with the web sockets when you app is open
- Use push notifications, so when you app is close, you get the new messages
Parse Server Live query did receive new objects when my app is close?
the answer is NO. parse live query is based on the WebSocket protocol so an active connection must be available in order to receive the events and app which are not in foreground cannot have an active connection to WebSocket. If your app will be in the background it will take about 1-2 minutes until the WebSocket connection will automatically be closed by the OS and this is done to save resources. So you can do what i did:
- When the app is in foreground and active use LiveQuery as normal and handle events inside the app
- When your app is in background or closed use push notifications to notify the user about important changes.
You can know if a user is online/offline by storing a flag of isOnline under the User object and when the app is active update this flag to true and when it is not active update this flag to false.
Related Topics
Default Uifont Size and Weight But Also Support Preferredfontfortextstyle
Xcode 8.3 Swift Version Error (Swift_Version) in Objective C Project
Spritekit Not Respecting Zposition
iOS 11 CPU Throttling and Idle Timer
How to Set Size Leftbarbuttonitem
Check the Position of the Xcuielement on Screen While Testing iOS Application Using Xctest
Swift Uiview Opacity Programmatically
iOS - Running Background Task for Update User Location Using Swift
Url Opening Swift App - Open Works - Called Function Does Not Work
How to Get Index of Xcuielement in Xcuielementquery
Wrong Text Height When Text Contains Emoji
Passing Data Between View Controllers: from UItableview to a Details View Controller
Programmatically Change Splash Screen in iOS