我有一个处理本地通知的类,我希望根据是否计划了任何通知来确定它们是否启用。因此,我想我可以尝试创建一个包含所有计划通知的数组的计算属性,但是我认为有一些事情我不理解,因为我得到了以下错误:
无法将“Void”类型的返回表达式转换为返回类型“UNNotificationRequest”
var notifications: [UNNotificationRequest] {
center.getPendingNotificationRequests { notifications in return notifications }
}只需在完成处理程序中打印通知就可以了,所以我能够正确地获得通知,只是不将它们分配给变量。
我还尝试创建一个单独的变量并返回该变量,但这始终默认为我提供的空默认值。
var notifications: [UNNotificationRequest] {
var retrievedNotifications: [UNNotificationRequest]?
center.getPendingNotificationRequests { notifications in retrievedNotifications = notifications }
return retrievedNotifications ?? []
}如有任何提示或提示,将不胜感激。
发布于 2020-05-12 01:03:29
您可能可以使用分派组,如下所示。实际上,您要做的是等待从线程检索所有通知,然后继续
var notifications: [UNNotificationRequest] {
var retrievedNotifications: [UNNotificationRequest] = []
let group = DispatchGroup()
group.enter()
// avoid deadlocks by not using .main queue here
DispatchQueue.global(attributes: .qosDefault).async {
center.getPendingNotificationRequests { notifications in
retrievedNotifications = notifications
group.leave()
}
}
// wait ...
group.wait()
return retrievedNotifications
}https://stackoverflow.com/questions/61741805
复制相似问题