gpt4 book ai didi

ios - 应用程序在前台时如何更新用户界面?

转载 作者:行者123 更新时间:2023-11-28 05:47:21 31 4
gpt4 key购买 nike

当我的应用程序处于事件状态并且我收到来自服务器的静默通知以使用新数据更新我的表格 View 时,我正在调用以下函数,我在其中向服务器发出请求以获取最新数据,然后重新加载该特定行。

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 say 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
}

}

关于ios - 应用程序在前台时如何更新用户界面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54179108/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com