解析云-LiveQueries-iOS客户端不起作用 [英] Parse Cloud - LiveQueries - iOS Client doesn't work
问题描述
我正在尝试使用Parse LiveQueries.
我使用此Parse"Bootstrap":" https://github.com/parse-community/parse-服务器",
我可以看到日志:info: Create new client: 1
,
但尽管我已订阅,但我只是未在查询中得到更新.它甚至没有到达subscription.handle
的处理程序.
I'm trying to use Parse LiveQueries.
I use this Parse "Bootstrap": "https://github.com/parse-community/parse-server",
I can see the logs: info: Create new client: 1
,
but I just do not get the update in the query although I have subscribed it. It doesn't even reach the handler of the subscription.handle
.
config.json
:
{
"appId": "",
"masterKey": "",
"appName": "",
"cloud": "./cloud/main",
"databaseURI": "",
"publicServerURL": "",
// Relevant
"startLiveQueryServer": true,
"liveQuery": {
"classNames": ["Channel"]
},
}
AppDelegate.swift
:
// Initialize Parse.
let configuration = ParseClientConfiguration {
$0.applicationId = self.PARSE_APP_ID
$0.server = self.PARSE_SERVER
}
Parse.initialize(with: configuration)
AppDelegate.liveQueryClient = ParseLiveQuery.Client()
The Subscription Code
(iOS Swift):
The Subscription Code
(iOS Swift):
public static func listenSubscribedChannels(handler: @escaping (_ channel: Channel) -> Void) {
var subscription: Subscription<PFObject>?
let query: PFQuery<PFObject> = PFQuery(className: "Channel").whereKey("subscribers", containedIn: [PFUser.current()!.objectId])
subscription = AppDelegate.liveQueryClient!.subscribe(query).handle(Event.updated) { _, channel in
handler(channel)
}
}
推荐答案
此代码的问题是您将此代码var subscription: Subscription<PFObject>?
放置在函数内部.
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.
例如.
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
}
})
}
}
从这段代码中可以看到,我将变量放置在函数定义之外,以便保留Subscription
的内存地址.
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.
这篇关于解析云-LiveQueries-iOS客户端不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!