'Unable to send data to the main thread. Android, tdlib
I'm trying to write a telegram-based mini-application. Connected tdlib (library for sending requests to telegram servers). All responses from the telegram server are returned to the overridden onResult() method of the class inherited from Client.ResultHandler.
class TelegramResultHandler: Client.ResultHandler {
private val _authState = MutableLiveData<Authentication>()
val authState: LiveData<Authentication> get() = _authState
override fun onResult(data: TdApi.Object) {
when (data.constructor) {
TdApi.UpdateAuthorizationState.CONSTRUCTOR -> {
onAuthorizationStateUpdated((data as TdApi.UpdateAuthorizationState).authorizationState)
}
else -> Log.d(TAG, "Unhandled call: $data.")
}
}
private fun onAuthorizationStateUpdated(authorizationState: TdApi.AuthorizationState) {
when (authorizationState.constructor) {
TdApi.AuthorizationStateWaitTdlibParameters.CONSTRUCTOR -> {
Log.d(TAG, "onResult: AuthorizationStateWaitTdlibParameters")
}
TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR -> {
Log.d(TAG, "onResult: AuthorizationStateWaitEncryptionKey")
}
TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR -> {
Log.d(TAG, "onResult: AuthorizationStateWaitPhoneNumber")
}
TdApi.AuthorizationStateWaitCode.CONSTRUCTOR -> {
Log.d(TAG, "onResult: AuthorizationStateWaitCode")
authState.postValue(Authentication.WAIT_FOR_CODE)
}
TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR -> {
Log.d(TAG, "onResult: AuthorizationStateWaitPassword")
authState.postValue(Authentication.WAIT_FOR_PASSWORD)
}
TdApi.AuthorizationStateReady.CONSTRUCTOR -> {
Log.d(TAG, "onResult: AuthorizationStateReady")
authState.postValue(Authentication.AUTHENTICATED)
}
TdApi.AuthorizationStateLoggingOut.CONSTRUCTOR -> {
Log.d(TAG, "onResult: AuthorizationStateLoggingOut")
}
TdApi.AuthorizationStateClosing.CONSTRUCTOR -> {
Log.d(TAG, "onResult: AuthorizationStateClosing")
}
TdApi.AuthorizationStateClosed.CONSTRUCTOR -> {
Log.d(TAG, "onResult: AuthorizationStateClosed")
}
else -> Log.d(TAG, "Unhandled authorizationState: $authorizationState.")
}
}
enum class Authentication {
UNAUTHENTICATED,
WAIT_FOR_NUMBER,
WAIT_FOR_CODE,
WAIT_FOR_PASSWORD,
AUTHENTICATED,
UNKNOWN
}
I am unable to pass this data to the main thread for display. For example, during authorization, after sending the code, the client can log in or request a cloud password, but information about this can only be obtained in onResult(), and it cannot be transmitted from there.
I tried to create a variable of the MutableLiveData type and send data using the postValue() method, but during the debugging process, I notice that the data has changed, but the subscriber does not react in any way. In a Fragment:
private val resultHandler = TelegramResultHandler()
resultHandler.authState.observe(viewLifecycleOwner){
when (it){
Authentication.WAIT_FOR_CODE -> {
showDialog("Enter confirmation code")
}
Authentication.WAIT_FOR_PASSWORD-> {
showDialog("Enter cloud password")
}
Authentication.AUTHENTICATED-> {
// request and display user data
}
else -> {
showDialog("Other")
}
}
}
How to pass data about any changes to the main thread to display information?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
