gpt4 book ai didi

ios - 使 getNotificationSettings 返回而不是使用完成 block

转载 作者:搜寻专家 更新时间:2023-10-31 22:09:46 25 4
gpt4 key购买 nike

我有一个方法用于我正在开发的应用程序的多个位置。这是一种检查远程推送通知是否启用的方法。该方法返回一个值,但您可能知道 currentUserNotificationSettings 已被弃用,所以现在我正在使用 getNotificationSettings

问题是第一个返回一个值,而最新的使用一个 block 。我仍然希望能够返回一个值以避免重构所有内容,所以我写了以下内容,但它失败了,我不明白为什么......

这样可以吗?!

public static var isRemoteEnabled: Bool {
var notificationSettings: UNNotificationSettings?
let semasphore = DispatchSemaphore(value: 2)

UNUserNotificationCenter.current().getNotificationSettings { setttings in
notificationSettings = setttings
semasphore.signal()
}

semasphore.wait()
guard let authorizationStatus = notificationSettings?.authorizationStatus else { return false }
return authorizationStatus == .authorized
}

编辑:

我关注了@rmaddy 的评论,至少现在它没有崩溃,但它卡在了 wait() 中。如果我转到调试器并 e semasphore.signal() 它完成并且应用程序继续正常工作。不知何故,完成 block 没有被调用。

最佳答案

在这种情况下,您希望创建的信号量初始值为 0,而不是 2

let semasphore = DispatchSemaphore(value: 0)

文档中提到了这一点:

Passing zero for the value is useful for when two threads need to reconcile the completion of a particular event.

wait 首先递减该值。然后它会阻塞,直到该值大于或等于 0。您的值为 2,它被递减为 1,因为它已经大于或等于 0,所以 wait 不需要阻塞并且您的方法在调用 signal 之前很久就返回了。

还有可能在同一线程上调用 getNotificationSettings 的完成 block (导致死锁),因此在后台队列上调用它。

public static var isRemoteEnabled: Bool {
var notificationSettings: UNNotificationSettings?
let semasphore = DispatchSemaphore(value: 0)

DispatchQueue.global().async {
UNUserNotificationCenter.current().getNotificationSettings { setttings in
notificationSettings = setttings
semasphore.signal()
}
}

semasphore.wait()
guard let authorizationStatus = notificationSettings?.authorizationStatus else { return false }
return authorizationStatus == .authorized
}

关于ios - 使 getNotificationSettings 返回而不是使用完成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50936584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com