Parse Cloud - Livequeries - iOS Client Doesn't Work

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:

  1. When the app is in foreground and active use LiveQuery as normal and handle events inside the app
  2. 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



Leave a reply



Submit