Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know when UNUserNotificationCenter's removeAllPendingNotificationRequests() has completed?

The iOS docs say that UNUserNotificationCenter's removeAllPendingNotificationRequests() is asynchronous.

What I want to do is this:

  1. Call removeAllPendingNotificationRequests() to get rid of all my scheduled notifications

  2. Schedule a bunch of new notifications, some of which may or may not have the same IDs as what was there previously

But since the documentation says that the method is asynchronously running on another thread (and there is no completion callback parameter) I'm worried that sometimes, depending on the vagaries of threads and timing and whatnot, that step #1 will still be going as I am creating things in step 2 and therefore it will also kill some of the new notifications I'm making.

This kind of stuff is a little tricky to test manually, since it depends on timing. So I'm curious is anyone knows if this is something I should be worried about or not...

like image 557
Erik Westwig Avatar asked Jul 23 '17 18:07

Erik Westwig


1 Answers

In documentation for add notification I found this:

Calling -addNotificationRequest: will replace an existing notification request with the same identifier.

Maybe the solution would be something like this:

  1. Create new notification requests
  2. Get all pending and filter out only the ones that will not be replaced
  3. Delete not replaced
  4. Add all new notifications
let center = UNUserNotificationCenter.current()
// Create new requests
let newRequests: [UNNotificationRequest] = [...]
let identifiersForNew: [String] = newRequests.map { $0.identifier }

center.getPendingNotificationRequests { pendingRequests in
   // Get all pending notification requests and filter only the ones that will not be replaced
    let toDelete = pendingRequests.filter { !identifiersForNew.contains($0.identifier) }
    let identifiersToDelete = toDelete.map { $0.identifier }

    // Delete notifications that will not be replaced
    center.removePendingNotificationRequests(withIdentifiers: identifiersToDelete)

     // Add all new requests
     for request in newRequests {
       center.add(request, withCompletionHandler: nil)
     }
}
like image 59
Nikola Ristic Avatar answered Sep 19 '22 18:09

Nikola Ristic