Skip to content

Commit

Permalink
Fix asking permission on IO thread crash
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnasSmicius committed Sep 20, 2023
1 parent b15df25 commit 7fbe19f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions library/src/main/java/com/vinted/coper/CoperImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import androidx.fragment.app.FragmentManager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlin.coroutines.resume


Expand Down Expand Up @@ -79,9 +81,11 @@ internal class CoperImpl(
}

internal suspend fun getFragmentSafely(): CoperFragment {
val fragment = getAttachedFragment()
fragment.lifecycle.waitOnCreate()
return fragment
return withContext(Dispatchers.Main) {
val fragment = getAttachedFragment()
fragment.lifecycle.waitOnCreate()
fragment
}
}

private suspend fun getAttachedFragment(): CoperFragment {
Expand Down

0 comments on commit 7fbe19f

Please sign in to comment.