Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Kotlin suspend functions from Swift/Objective-C is currently supported only on main thread

I am trying to delete the fcm token from a user, and then logout.

But I cant do that because I receive the error 'Calling Kotlin suspend functions from Swift/Objective-C is currently supported only on main thread'.

It seems that somehow the suspend function will suspend the next block?

The repo.deleteToken method:

 suspend fun deleteToken(token: String) {
        val userId = appService.currentUser!!.id
            realm.write {
                var user = query<UserInfo>("_id = $0", userId).first().find()
                if (user != null) {
                    user = findLatest(user)!!.also {
                        it.FCMToken.remove(token)
                    }
                    copyToRealm(user)
                }
        }
    }

and the repo.logout:

suspend fun doLogout() {
        appService.currentUser?.logOut()
    }

My logout method that deletes the token and tries to logout:

func doLogout(){
        
        repo.deleteToken(token: myFCMToken){error in
            isLoginShown = true

            repo.doLogout(){error in
                
            }
        }
    }

When I do the logout: *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Calling Kotlin suspend functions from Swift/Objective-C is currently supported only on main thread'

I know that there is a flag that can avoid that but I do not want to use that, I do not want to risk changing this.

How can I change the logout flow to first delete and then logout?

like image 625
dhaval123 Avatar asked Sep 01 '25 10:09

dhaval123


1 Answers

This restriction was made for old memory-model. If you use new memory model, you can disable this check in gradle.properties

kotlin.native.binary.objcExportSuspendFunctionLaunchThreadRestriction=none

You use new memory if you have kotlin 1.7.0+ and kotlinx-coroutine 1.6.0+ and not native-mt-version.

More details here: https://youtrack.jetbrains.com/issue/KT-51297/Native-allow-calling-Kotlin-suspend-functions-on-non-main-thread-from-Swift

like image 195
Pavel Shorokhov Avatar answered Sep 03 '25 22:09

Pavel Shorokhov