【发布时间】:2019-07-05 17:27:20
【问题描述】:
我的应用中有一个非常小的聊天。每当用户收到消息时,服务器都会发送通知。在 appDelegate 中,我实现了以下代码:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
// print(UNNotificationAction)
print("???????????? recieved")
if notification.request.content.title.contains("Message")
{
print("sub")
print(notification.request.content.subtitle)
print("body")
print(notification.request.content.body)
print("it containt DM ⏰")
let storyboard:UIStoryboard = UIStoryboard(name:"Chat", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "chatRoomVc") as! ChatRoomViewController
vc.becomeFirstResponder()
vc.setRefreshListener {
print("triggered")
}
}
}
无论何时运行,它都会调用 API 并获取聊天数据。 下面的代码展示了它是如何获取数据的:
func setRefreshListener(listener:@escaping ()->Void){
refreshListener = listener
presenter.getttingChatRoomData(aptId: UserDefaults.standard.string(forKey: userAPTID)!, id: UserDefaults.standard.string(forKey: userID)!)
presenter.attachView(view: self)
super.loadView()
super.reloadInputViews()
tableView.reloadData()
}
另外,我可以正常获取数据。
问题是它没有为表视图重新加载数据!!
有没有人知道为什么会这样?
更新: 下面的代码在收到数据时运行:
func gettingUserChatWithSUCCESS(responce: [chatRomModel]) {
print("✅ getting chat room data SUCCESS")
data = responce
tableView.reloadData()
}
【问题讨论】:
-
当你从 API 收到数据时重新加载数据,实现 clsoure 以获取你的 api 数据到达的通知,然后你可以更新你的 tableview。
-
@ShauketSheikh 更新了问题,我忘记了将其添加到我的问题中的部分。我还添加了一个断点,但它不会重新加载表!
-
在 DispatchQueue.main.async { } 中重新加载数据
-
每次你创建不同的 ChatRoomViewController 对象时,如果你想在同一个视图控制器上重新加载它是行不通的,只需获取根视图控制器并重新加载它。
-
您每次都以错误的方式使用它来创建 VC 吗?并检查您的 tableView numberOfRows 委托方法是否被调用?
标签: ios swift uitableview push-notification listener