@objc func removeAllMeasurementReminder() { var removeIdentifiers = [String]() for i in 0...6 { for j in 0...9 { if #available(iOS 10.0, *) { removeIdentifiers.append(HW_BLOOD_PRESSURE_MEASUREMENT_NOTIFICATION + "\(i)" + "\(j)") } else { guard let allNotifications = UIApplication.shared.scheduledLocalNotifications else { return } for notification in allNotifications { let info = notification.userInfo as? [String: String] ?? ["":""] let identifier = info[HW_BLOOD_PRESSURE_MEASUREMENT_NOTIFICATION + "\(i)" + "\(j)"] if ((identifier?.hasPrefix(HW_BLOOD_PRESSURE_MEASUREMENT_NOTIFICATION)) != nil) { UIApplication.shared.cancelLocalNotification(notification) } } } } } if #available(iOS 10.0, *) { UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: removeIdentifiers) } }函数内部操作移到子线程
时间: 2024-01-07 16:03:28 浏览: 29
可以将函数内部的操作移到一个异步的子线程中执行,避免在主线程中进行耗时操作导致界面卡顿。可以使用GCD的异步任务来实现:
```
@objc func removeAllMeasurementReminder() {
DispatchQueue.global(qos: .background).async {
var removeIdentifiers = [String]()
for i in 0...6 {
for j in 0...9 {
if #available(iOS 10.0, *) {
removeIdentifiers.append(HW_BLOOD_PRESSURE_MEASUREMENT_NOTIFICATION + "\(i)" + "\(j)")
} else {
guard let allNotifications = UIApplication.shared.scheduledLocalNotifications else {
return
}
for notification in allNotifications {
let info = notification.userInfo as? [String: String] ?? ["":""]
let identifier = info[HW_BLOOD_PRESSURE_MEASUREMENT_NOTIFICATION + "\(i)" + "\(j)"]
if ((identifier?.hasPrefix(HW_BLOOD_PRESSURE_MEASUREMENT_NOTIFICATION)) != nil) {
UIApplication.shared.cancelLocalNotification(notification)
}
}
}
}
}
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: removeIdentifiers)
}
}
}
```