等到使用 removePendingNotificationRequests ios 10 swift 3 删除来自 UNUserNotificationCenter 的本地通知

Igo*_*nko 5 ios swift3 ios10 unusernotificationcenter

使用来自UNUserNotificationCenter. 我尝试删除带有一些标识符的通知:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
Run Code Online (Sandbox Code Playgroud)

并从文档:

此方法异步执行,删除辅助线程上挂起的通知请求。

完成处理程序不存在。那么我怎么知道它什么时候真的被删除了呢?在继续之前,我需要确保此标识符不再存在。

我知道我可以使用下一个代码

notificationCenter.getPendingNotificationRequests { (requests) in
        for request in requests {
         }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我在删除后立即运行此代码 - 它们仍然存在。但是一段时间后,在代码中它们消失了。当您即将达到 64 条通知的限制时,在添加新通知之前尤其重要

Igo*_*nko -2

好吧,看起来过了一会儿我发现了其中一种方法 - 它也是DispatchGroup 以异步方式完成的,并且具有userInteractive质量:

let dq = DispatchQueue.global(qos: .userInteractive)
dq.async {
    let group  = DispatchGroup()
    group.enter()
    UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arr)
group.leave()

    group.notify(queue: DispatchQueue.main) { () in
        UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
            for request in requests {
                print("||=> Existing identifier is \(request.identifier)")
            }
        }
    }        
}
Run Code Online (Sandbox Code Playgroud)

并且删除的通知不存在于getPendingNotificationRequests

  • 我认为它不起作用,因为removePendingNotificationRequests仍将在不同的线程上运行,因此该组将立即离开。如果在removePendingNotificationRequests所在的线程中调用group.leave,它将起作用 (2认同)