等待直到使用removePendingNotificationRequests iOS 10 swift 3删除来自UNUserNotificationCenter的本地通知 [英] Wait till local notifications from UNUserNotificationCenter gets deleted using removePendingNotificationRequests ios 10 swift 3

查看:866
本文介绍了等待直到使用removePendingNotificationRequests iOS 10 swift 3删除来自UNUserNotificationCenter的本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using new local notifications from UNUserNotificationCenter. I try to delete notification with some identifiers:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)

和来自文献:

此方法异步执行,删除辅助线程上的待处理通知请求.

This method executes asynchronously, removing the pending notification requests on a secondary thread.

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

Completion handler is not present. So how do I know when its really get deleted? Before moving ahead, I need to make sure that this identifier is not present anymore.

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

I know I can use next code

notificationCenter.getPendingNotificationRequests { (requests) in
        for request in requests {
         }
}

但是,如果我在删除后立即运行此代码-它们仍然存在.但是经过一段时间和以后的代码,它们就消失了.尤其重要,当您要丰富64条通知的限制时,在添加新通知之前

But if I run this code right after removing - they are still there. But after some time and later in the code they are gone. Especially its important before adding new one when you re about to rich the limit of 64 notifications

推荐答案

好一阵子后,我发现其中一种方法-它的DispatchGroup 它也以userInteractive质量以异步方式完成:

Ok looks like after awhile I found one of the ways - its DispatchGroup Its done also in async way with userInteractive quality:

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)")
            }
        }
    }        
}

getPendingNotificationRequests

这篇关于等待直到使用removePendingNotificationRequests iOS 10 swift 3删除来自UNUserNotificationCenter的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆