Parse Cloud - LiveQueries - iOS 客户端不工作
Parse Cloud - LiveQueries - iOS Client doesn't work
我正在尝试使用 Parse LiveQueries。
我使用这个解析 "Bootstrap": "https://github.com/parse-community/parse-server",
我可以看到日志:info: Create new client: 1
、
但我只是没有得到查询中的更新,尽管我已经订阅了它。它甚至没有到达 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 {
[=12=].applicationId = self.PARSE_APP_ID
[=12=].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)
}
}
查询前需要先注册class,例如:
Channel.registerSubclass()
此代码的问题是您将此代码 var subscription: Subscription<PFObject>?
放在函数中。
此对象必须能够保留其内存地址才能接收事件。
例如
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
的内存地址,我已将变量放在函数定义之外。
我正在尝试使用 Parse LiveQueries。
我使用这个解析 "Bootstrap": "https://github.com/parse-community/parse-server",
我可以看到日志:info: Create new client: 1
、
但我只是没有得到查询中的更新,尽管我已经订阅了它。它甚至没有到达 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 {
[=12=].applicationId = self.PARSE_APP_ID
[=12=].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)
}
}
查询前需要先注册class,例如:
Channel.registerSubclass()
此代码的问题是您将此代码 var subscription: Subscription<PFObject>?
放在函数中。
此对象必须能够保留其内存地址才能接收事件。
例如
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
的内存地址,我已将变量放在函数定义之外。