I’m using ParseLiveQuery-iOS-OSX library to receive live server updates from my Parse Server in a SwiftUI project. I’ve followed closely to various sources online. Below is my current viewmodel:
class InitializationViewModel: ObservableObject{
var subscription: Subscription<PFObject>?
var subscriber = Client(server: "ws://xx.xx.xx.xx", applicationId: "xxxx", clientKey: "]")
let query : PFQuery<PFObject> = Chats.query()!.whereKey("users", contains: "user1")
init(){
self.subscribeToChats()
}
func subscribeToChats() {
self.subscriber.shouldPrintWebSocketLog = false
self.subscriber.shouldPrintWebSocketTrace = true
self.subscription = subscriber.subscribe(query).handle(Event.created) { query, object in
print(object)
}
}
}
Since I turned on client tracing, i’m able to see incoming json live query objects from my server, so that part is all working. However, I’m the event handler just won’t be triggered.
I did some further investigation, and find out that error appears in line 23 of ParseLiveQuery/ClientPrivate.swift,
guard let object = PFDecoder.object().decode(objectDictionary) as? T else {
throw LiveQueryErrors.InvalidJSONObject(json: objectDictionary, details: "cannot decode json into \(T.self)")
}
The decoding part (PFDecoder.object().decode(objectDictionary)
) works ok. But the error appears when it tries to cast the decoded object to the type T
, which is the type PFObject
in my case (Type def here). I checked the decoded object indeed contains all the required fields of a PFObject
. So I’m kinda stuck on why this happens / whether there’s a way to fix this problem.
Any help is appreciated