I am trying to program a function which returns all the identifiers of pending notifications, which are repeating.
func getRepeatingNotificationsIds () -> [String] {
var repatingNotification:[String] = []
UNUserNotificationCenter.current().getPendingNotificationRequests {
(requests) in
for request in requests{
if (request.trigger?.repeats == true)
{
repatingNotification.append(request.identifier)
}
}
}
return repatingNotification
}
However the repatingNotification array remains empty when returned. Is it possible to maybe call repatingNotification by reference or something?
CodePudding user response:
Rather than trying to return a value, tell your function what you want it to do after collecting the requests by sending it a completion block. As mentioned in a comment, what you have won't work because of timing.
Here's a trivial playground example that should give you the idea.
import UIKit
func getRepeatingNotificationsIds (completion: @escaping ([String])->()) {
var repatingNotification:[String] = []
UNUserNotificationCenter.current().getPendingNotificationRequests {
(requests) in
for request in requests {
if (request.trigger?.repeats == true) {
repatingNotification.append(request.identifier)
}
}
completion(repatingNotification)
}
}
getRepeatingNotificationsIds { notifications in
print(notifications)
}
