刷新jsqmessgeviewcontroller

时间:2016-04-27 09:48:41

标签: ios swift jsqmessagesviewcontroller

我创建了一个包含多个视图的小型iOS应用。还有一个聊天视图,我使用jsqmessagesViewVontroller。 我已经实现了推送通知来调用一个方法,该方法要求我的服务器获取新消息。 该方法在chatController中。 收到推送通知时,将设置具有属性观察者的单例属性。 Observer创建Controller的新对象,设置所需的变量并调用receiveChat()方法。 到目前为止一切正常。但是当打开chatController并且新的推送通知收到时,聊天将不会被刷新。 我也试图通过NSNotificationCenter实现它,但它也没有为我工作。 如何在打开的聊天视图中调用相应聊天的新消息并刷新视图以便显示新消息或在聊天视图打开时告知聊天视图应将聊天消息从CoreData重新加载到刷新聊天?

我的物业观察员:

var newChat: Bool = false {
    didSet {
        let vc = ChatController()
        vc.setupClass()
        vc.receiveChat()
    }
}

我的AppDelegate处理通知(包括我测试NSNotificationCenter方法的行):

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
  NSNotificationCenter.defaultCenter().postNotificationName(NSNotificationcenterKeys().reloadChatNotificationKey, object: self)
    //sharedData.newChat = true
}

ChatController注册NSNotification Observer:

    func registerObserver() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChatController.doNotification(_:)), name: NSNotificationcenterKeys().reloadChatNotificationKey, object: nil)
}

ChatController NSNotification方法:

    func doNotification(note: NSNotification) {
    receiveChat()
}

ChatController接收消息方法:

    func receiveChat() {

... prepare the request...
    Alamofire.request(.GET, req, parameters: parameters, headers: authHeader).validate().responseJSON {
        response in
        debugPrint(response)
        if response.result.error == nil {
... a lot json stuff happens here...
                        self.dataManager.save()
                        if self.chat.serverId == chatId {
                            self.addMessage(author, text: nachricht, date: NSDate(timeIntervalSince1970: timestamp/1000))
                            self.finishReceivingMessage()
                        }

                    } catch {
                        print(error)
                    }
                }

0 个答案:

没有答案