Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type mismatch required LiveData<TypeVariable(T)> found Unit #4

Closed
abdulmalekDery opened this issue May 3, 2020 · 2 comments
Closed

Comments

@abdulmalekDery
Copy link

abdulmalekDery commented May 3, 2020

I'm trying to understanding this architecture by coding example and I followed the project set up
till I stuck with that compile time error

Type mismatch required LiveData<TypeVariable(T)> found Unit

in the viewModel I have code like that for register request

 fun postRegisterRequest(request: RegisterRequest) {
    this.registerLiveData =  launchOnViewModelScope {
            this.authRepository.postRegister ({ s -> this.toastLiveData.postValue(s) },
                request
            )
        }
}

in the repository I have the following code

suspend fun postRegister(error: (String) -> Unit, request: RegisterRequest) = withContext(Dispatchers.IO) {
    val liveData = MutableLiveData<ResponseWrapper<RegisterResponse>>()
    isLoading = true
    networkClient.postRegister( { response ->
        isLoading = false
        when (response) {
            is ApiResponse.Success -> {
                response.data.whatIfNotNull {
                    liveData.postValue(it)
                }
            }
            is ApiResponse.Failure.Error -> error(response.message())
            is ApiResponse.Failure.Exception -> error(response.message())
        }
    },request)
}

the network client request look like that

 fun postRegister(
    onResult: (response: ApiResponse<ResponseWrapper<RegisterResponse>>) -> Unit,
    request: RegisterRequest
) {
    this.authService.register(request).transform(onResult)
}

the authService code

  @POST(Urls.REGISTER)
fun register(@Body request: RegisterRequest): Call<ResponseWrapper<RegisterResponse>>

what I did wrong and how to make postRegister in the repository return LiveData<TypeVariable(T)> instead of Unit

@skydoves
Copy link
Owner

skydoves commented May 5, 2020

@abdulmalekDery
Hi, you can return the liveData in the coroutines context.

suspend fun postRegister(error: (String) -> Unit, request: RegisterRequest) = withContext(Dispatchers.IO) {
    val liveData = MutableLiveData<ResponseWrapper<RegisterResponse>>()
    // skip stubs
    liveData // return liveData
}

@abdulmalekDery
Copy link
Author

thanks I will try that

@skydoves skydoves closed this as completed May 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants