Nicolas Patin
10/25/2021, 10:05 AMactual suspend fun areNotificationsSystemEnabled() : Boolean = suspendCoroutine { continuation ->
val settingsCompletionHandler = { settings: UNNotificationSettings? ->
val enabled = settings?.authorizationStatus == UNAuthorizationStatusAuthorized
continuation.resume(enabled)
}.freeze()
val notificationCenter = UNUserNotificationCenter.currentNotificationCenter()
notificationCenter.getNotificationSettingsWithCompletionHandler(settingsCompletionHandler)
}
My problem here is that the “continuation” used inside the completion handler is frozen, so it crashes on the continuation.resume(enabled)
:
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.coroutines.SafeContinuation@80487928
[...]
at 7 KMM 0x00000001071d43e0 kfun:kotlin.coroutines.SafeContinuation#resumeWith(kotlin.Result<1:0>)
I think my issue is more or less the same than this one : https://youtrack.jetbrains.com/issue/KT-43566
I don’t know if I should ask this on #coroutines but if anyone already hit this issue, I’ll be glad to hear from you 😄
Thanks !russhwolf
10/25/2021, 12:27 PMCompletableDeferred
instead of suspendCoroutine
tends to work better if you're running into freezing issuesNicolas Patin
10/25/2021, 1:14 PMactual suspend fun areNotificationsSystemEnabled() : Boolean {
val deferred = CompletableDeferred<Boolean>()
val settingsCompletionHandler = { settings: UNNotificationSettings? ->
val enabled = settings?.authorizationStatus == UNAuthorizationStatusAuthorized
deferred.complete(enabled)
// returns Unit to avoid error "the feature "Unit conversion" is disabled"
Unit
}.freeze()
val notificationCenter = UNUserNotificationCenter.currentNotificationCenter()
notificationCenter.getNotificationSettingsWithCompletionHandler(settingsCompletionHandler)
return deferred.await()
}