如何在应用程序处于前台时更新 UI?
How to update UI when app is in foreground?
当我的应用处于活动状态并且我收到来自服务器的静默通知以使用新数据更新我的 tableview 时,我调用了以下函数,我在其中向服务器发出请求以获取最新数据然后重新加载该特定行。
func updateCell(path: Int, messageId: String) {
let indexPath = IndexPath(item: path, section: 0)
if let visibleIndexPaths = mainTableView.indexPathsForVisibleRows?.index(of: indexPath as IndexPath) {
if visibleIndexPaths != NSNotFound {
if let id = self.userData?.id {
let conversationID = String(describing: id)
ServerConnection.getSpecificMessage(conversationId: conversationID, messageId: messageId) { (dataMessage) in
if let message = dataMessage {
self.chat[path] = message
self.mainTableView.beginUpdates()
self.mainTableView.reloadRows(at: [indexPath], with: .fade)
self.mainTableView.endUpdates()
}
}
}
}
}
}
我的问题是,当我的应用程序在前台时,流程不再工作,因为无法在前台/后台完成的 API 请求。
控制台日志显示:
load failed with error Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort"
我尝试用
修改我的函数
let state = UIApplication.shared.applicationState
if state == .background || state == .inactive {
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData(_:)), name: NSNotification.Name("BackgroundReload"), object: nil)
}
并在 AppDelegate
中发布了这条 "BackgroundRelod" 通知
func applicationWillEnterForeground(_ application: UIApplication)
但这将始终触发我的功能,即使我没有收到任何静默通知来更新 UI。
你不应该在更新中依赖后台模式,你只需要修改一个 var 说 needsUpdate
每当后台出现静默通知时
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
然后
NotificationCenter.default.addObserver(self, selector: #selector(update), name: UIApplication.willEnterForegroundNotification, object: nil)
@objc func ppp(_ no:NSNotification) {
if needsUpdate {
// pull here
}
}
当我的应用处于活动状态并且我收到来自服务器的静默通知以使用新数据更新我的 tableview 时,我调用了以下函数,我在其中向服务器发出请求以获取最新数据然后重新加载该特定行。
func updateCell(path: Int, messageId: String) {
let indexPath = IndexPath(item: path, section: 0)
if let visibleIndexPaths = mainTableView.indexPathsForVisibleRows?.index(of: indexPath as IndexPath) {
if visibleIndexPaths != NSNotFound {
if let id = self.userData?.id {
let conversationID = String(describing: id)
ServerConnection.getSpecificMessage(conversationId: conversationID, messageId: messageId) { (dataMessage) in
if let message = dataMessage {
self.chat[path] = message
self.mainTableView.beginUpdates()
self.mainTableView.reloadRows(at: [indexPath], with: .fade)
self.mainTableView.endUpdates()
}
}
}
}
}
}
我的问题是,当我的应用程序在前台时,流程不再工作,因为无法在前台/后台完成的 API 请求。
控制台日志显示:
load failed with error Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort"
我尝试用
修改我的函数let state = UIApplication.shared.applicationState
if state == .background || state == .inactive {
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData(_:)), name: NSNotification.Name("BackgroundReload"), object: nil)
}
并在 AppDelegate
中发布了这条 "BackgroundRelod" 通知func applicationWillEnterForeground(_ application: UIApplication)
但这将始终触发我的功能,即使我没有收到任何静默通知来更新 UI。
你不应该在更新中依赖后台模式,你只需要修改一个 var 说 needsUpdate
每当后台出现静默通知时
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
然后
NotificationCenter.default.addObserver(self, selector: #selector(update), name: UIApplication.willEnterForegroundNotification, object: nil)
@objc func ppp(_ no:NSNotification) {
if needsUpdate {
// pull here
}
}