gpt4 book ai didi

ios - 每次将应用程序推回前台时刷新 tableView 的最有效方法是什么?

转载 作者:行者123 更新时间:2023-11-28 09:48:53 25 4
gpt4 key购买 nike

目前我拥有的是:

AppDelegate.applicationDidBecomeActive():

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
guard let vc = self.window?.rootViewController?.children.first as! AlarmTableViewController? else {
fatalError("Could not downcast rootViewController to type AlarmTableViewController, exiting")
}
vc.deleteOldAlarms(completionHandler: { () -> Void in
vc.tableView.reloadData()
})
}

删除旧警报():

func deleteOldAlarms(completionHandler: @escaping () -> Void) {

os_log("deleteOldAlarms() called", log: OSLog.default, type: .default)
let notificationCenter = UNUserNotificationCenter.current()
var activeNotificationUuids = [String]()
var alarmsToDelete = [AlarmMO]()
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext

notificationCenter.getPendingNotificationRequests(completionHandler: { (requests) in
for request in requests {
activeNotificationUuids.append(request.identifier)
}
for alarm in self.alarms {
guard let alarmUuids = alarm.value(forKey: "notificationUuids") as! [String]? else {
os_log("Found nil when attempting to unwrap notificationUuids in deleteOldAlarms() in AlarmTableViewController.swift, cancelling",
log: OSLog.default, type: .default)
return
}
let activeNotificationUuidsSet: Set<String> = Set(activeNotificationUuids)
let alarmUuidsSet: Set<String> = Set(alarmUuids)
let union = activeNotificationUuidsSet.intersection(alarmUuidsSet)
if union.isEmpty {
alarmsToDelete.append(alarm)
}
}
os_log("Deleting %d alarms", log: OSLog.default, type: .debug, alarmsToDelete.count)
for alarmMOToDelete in alarmsToDelete {
self.removeNotifications(notificationUuids: alarmMOToDelete.notificationUuids as [String])
managedContext.delete(alarmMOToDelete)
self.alarms.removeAll { (alarmMO) -> Bool in
return alarmMOToDelete == alarmMO
}
}
completionHandler()
})

}

但感觉很恶心。另外,我现在在后台线程(执行完成处理程序的线程)上调用 tableView.reloadData() 。用户打开应用程序备份后刷新 UI 的最佳方式是什么?我的目标是删除这些旧警报并重新加载 View 。如果通知中心没有任何待处理的通知(意味着通知已经执行),则该警报被视为旧警报。

最佳答案

不要在应用委托(delegate)中放置任何代码。让 View Controller 注册以在应用进入前台时接收通知。

viewDidLoad 中添加:

NotificationCenter.default.addObserver(self, selector: #selector(enteringForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

然后添加:

@objc func enteringForeground() {
deleteOldAlarms {
DispatchQueue.main.async {
tableView.reloadData()
}
}
}

从 iOS 13 开始,您应该注册 UIScene.willEnterForegroundNotification。如果您的应用需要在 iOS 13 和 iOS 12 下运行,那么您需要注册这两个通知,但您可以使用相同的选择器。

关于ios - 每次将应用程序推回前台时刷新 tableView 的最有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53585645/

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