Etg*_*gar 7 livequery ios parse-platform swift parse-cloud
我正在尝试使用Parse LiveQueries.我使用这个Parse"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"]
},
}
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
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)
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码的问题是您正在将该代码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
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
从这段代码中可以看到,我将变量放置在函数定义之外,以便Subscription保留的内存地址。