Parse Cloud - LiveQueries - iOS Client doesn't work

我的梦境 提交于 2019-11-28 12:16:37

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.

You have to register your class before query, like:

Channel.registerSubclass()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!