如何使用PromiseKit和Moya取消长轮询请求

时间:2017-09-07 09:55:55

标签: ios swift long-polling promisekit moya

我使用PromiseKit和Moya在下面的方法中发送长轮询请求:

func pullMessages() {

    let service = ChatServices()
    let request = service.pullMessages()
    self.request = request
    request.promise.then { [weak self] status -> Void in

        // If chat successfully established with the agent
        // navigate to chat screen or else try again. And if failure then show error message

        guard let `self` = self else { return }

        switch status {

        case .waiting:
            self.pullMessages()

        case .messages(let messages):
            for message: String in messages {
                self.addMessage(text: message, sender: self.agentSender())
            }
            self.pullMessages()

        case .chatEnded(let reason):
            if reason  == .agent {
                self.endChat(with: "Agent ended chat session")
            }

        case .failure:
            self.endChat(with: "Session lost")
        }

        }.catch { error in
            // Show error
            Log.warning(error.localizedDescription)
            self.endChat(with: "Session lost")
    }

}

我在viewWillDisapear离开视图后,使用self.request取消请求。 但是在我离开视图后,一条新消息到了。 "取消"请求返回消息。任何想法为什么会发生这种情况?

0 个答案:

没有答案