how do i wait for an asynchronous process to finish in kotlin compose without blocking ui thread?

I am trying to delete user's data when user logout in local storage. logout popup comes out if user wants to logout and logout popup has two button yes and no.clearLocalStorage function is running if user click yes and navigate the start screen but since it navigates without waiting for deletion , when I debug it does not delete user data from local . How can i solve this problem. I used runBlocking then it worked but it also blocks the ui thread. deleting in local storage with tokenManager or preferenceDataStoreHelper is working async as you know

hear is my code:

viewmodel

I tried something but if it's wrong then ignore it

@HiltViewModel
class SettingsViewModel @Inject constructor(
    private val tokenManager: TokenManager,
    private val preferenceDataStoreHelper: PreferenceDataStoreHelper
) : ViewModel() {

    suspend fun cleanLocalStorage() {
        tokenManager.deleteAccessToken()
        tokenManager.deleteRefreshToken()
        preferenceDataStoreHelper.removePreference(PreferenceDataStoreConstants.USER_EMAIL)
        preferenceDataStoreHelper.removePreference(PreferenceDataStoreConstants.USER_NAME)
        preferenceDataStoreHelper.removePreference(PreferenceDataStoreConstants.IS_USER_CHOOSE_CM)
        preferenceDataStoreHelper.removePreference(PreferenceDataStoreConstants.IS_USER_CHOOSE_FT)

    }
}

screen

 LaunchedEffect(key1 = state.shouldSignOut, block = {
        if (state.shouldSignOut) {
            navHostController.navigate(RegisterScreen.SignUpStartRegisterScreen.route)
        }
    })
    val coroutineScope = rememberCoroutineScope()
 if (state.shouldShowSignOutPopUp) {
        SignOutPopUp(
            isClickedYes = {
                coroutineScope.launch {
                    cleanLocalStorage()
                }
                shouldSignOut(true)
                shouldShowSignOutPopUp(false)
            }, isClickedNo = {
                shouldSignOut(false)
                shouldShowSignOutPopUp(false)
            }, text = "Çıkış yapmak istediğinize emin misiniz ?"
        )
    }


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation