问题描述
我正在尝试使用 Parse LiveQueries.我使用这个解析引导程序":https://github.com/parse-community/parse-服务器",
我可以看到日志:info: Create new client: 1
,
但我只是没有在查询中获得更新,尽管我已经订阅了它.它甚至没有到达 subscription.handle
的处理程序.
config.json
:
{"appId": "",主密钥": "","应用名称": "","cloud": "./cloud/main","databaseURI": "","publicServerURL": "",//相关的startLiveQueryServer":真,实时查询":{类名":[频道"]},}
AppDelegate.swift
:
//初始化解析.让配置 = ParseClientConfiguration {$0.applicationId = self.PARSE_APP_ID$0.server = self.PARSE_SERVER}Parse.initialize(with: 配置)AppDelegate.liveQueryClient = ParseLiveQuery.Client()
订阅代码
(iOS Swift):
public static func listenSubscribedChannels(handler: @escaping (_ channel: Channel) -> Void) {var 订阅:订阅?让查询:PFQuery= PFQuery(className: "Channel").whereKey("subscribers", containsIn: [PFUser.current()!.objectId])订阅 = AppDelegate.liveQueryClient!.subscribe(query).handle(Event.updated) { _, channel in处理程序(通道)}}
这段代码的问题是你把这段代码 var subscription: Subscription?
放在函数里面.>
该对象必须能够保留其内存地址才能接收事件.
例如.
class SomeClass {var 对象:[PFObject] = []var 订阅:订阅?var 订阅者:ParseLiveQuery.Client!让查询 = PFQuery(className: "Locations")功能开始监听器(){//初始化客户端订阅者 = ParseLiveQuery.Client()//初始化订阅者并开始订阅订阅 =subscriber.subscribe(conversationQuery)//处理事件监听器._ = 订阅?.handleEvent({ (_, event) in切换事件{案例.created(让对象):self.objects.append(object)//做东西默认:break//做其他事情或什么都不做}})}}
从这段代码中可以看出,为了保留Subscription
的内存地址,我将变量放在函数定义之外.
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):
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)
}
}
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 Cloud - LiveQueries - iOS 客户端不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!