我在端口9091上配置了在Amazon EC2实例上运行LiveQuery的解析服务器,没有代理,进行了防火墙配置。它似乎运行正常,因为我们在React上测试并使用http://www.websocket.org/echo.html,告诉它连接成功。
然而,在iOS上,我无法使LiveQuery正常工作,即使handleSubscribe似乎没问题。
我创建了一个简单的viewController来尝试它:
import UIKit
import Parse
import ParseLiveQuery
class StatsViewController: UIViewController {
@IBAction func insertMessage(sender: AnyObject) {
let message = Message();
message.author = PFUser.currentUser()
message.authorName = message.author?.username
message.text = "Hello: \(NSDate())"
message.roomName = "Test";
message.saveInBackground();
}
var myQuery: PFQuery {
return (Message.query()?
.whereKey("roomName", equalTo: "2")
.orderByAscending("createdAt"))!
}
var subscription: Subscription<Message>? = nil
override func viewDidLoad() {
super.viewDidLoad()
//Registers the subclass
Message.registerSubclass();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
//1) queries for messages, working.
myQuery.findObjectsInBackground().continueWithBlock() { res in
for msg in ((res.result as? [Message]))! {
print(msg);
}
return nil
}
//2) try to subscribe and receive any information from the server
subscription = myQuery.subscribe()
subscription!.handleEvent { query, event in
// Handle event
print("any event, please?"); //no events handled after creating a new message
}
subscription!.handle(Event.Created) { query, object in
// Called whenever an object was created
print("any creation, please?"); //no events handled after creating a new message
}
subscription!.handleError { (query, err) in
print("any error, please?"); //no errors handled
}
subscription!.handleSubscribe { query in
print("subscribed")
}
}
}
我也尝试使用解析示例中的代码进行订阅,例如
func subscribeToUpdates() {
subscription = liveQueryClient
.subscribe(messagesQuery)
.handle(Event.Created) { _, message in
self.printMessage(message)
}
}
订阅过程中是否有任何错误或遗漏?
谢谢!
答案 0 :(得分:1)
尝试添加
Message.registerSubclass();
在Parse配置设置之前
答案 1 :(得分:0)
我在Xcode 8.1 iOS 10.1中使用它,它可以工作。
str