代码之家  ›  专栏  ›  技术社区  ›  sadat

如何在viewmodel LiveData+Courotine+MVVM+改造中观察数据

  •  0
  • sadat  · 技术社区  · 6 年前

    问题是我无法在viewModel中使用LiveData和Courotine获得响应。可能我不知道该怎么做。电话是

    interface AuthApiService {
        @POST("v2/5e3cba6a2d00008709d958d0")
        @FormUrlEncoded
        suspend fun login(
            @Field("username") username: String,
            @Field("password") password: String
        ): Response<AuthToken>
    }
    

    存储库是

    class AuthRepository
    @Inject
    constructor(
        val authApiService: AuthApiService
    ) {
        suspend fun login(username: String, password: String) = liveData {
            emit(GenericResult.Loading(null))
            try {
                emit(GenericResult.Success(authApiService.login(username, password)))
            } catch (ioException: Exception) {
                emit(GenericResult.Error(ioException))
            }
        }
    }
    

    我想在viewmodel中做的是,

    viewModelScope.launch {
                val result = authRepository.login(username, password)
    
                when (result.value) {
                    is GenericResult.Loading -> isLoading.postValue(true)
                    is GenericResult.Success -> authToken.postValue((result.value as GenericResult.Success<Response<AuthToken>>).data.body())
                    is GenericResult.Error -> onMessageError.postValue((result.value as GenericResult.Error).exception)
                }
            }
    

    而且它不起作用。你能告诉我我做错了什么吗?谢谢

    0 回复  |  直到 6 年前
        1
  •  1
  •   Emmanuel    6 年前

    在我看来,你没有得到结果的原因是因为没有 Observers 对于 LiveData 归还人 authRepository.login(username, password)

    你需要做一些事情,比如:

    val result = authRepository.login(username, password)
    result.observe(someLifeCycleOwner, Observer {...})
    

    这通常发生在一个房间里 Fragment 或 Activity 哪些是 LifeCyclerOwners .

    而且 liveData {...} 需要一个 suspend 块,但它本身不是一个 悬 作用这意味着 login() 不需要成为一个 悬 作用

        2
  •  0
  •   Patibandha Parth    6 年前

    查看下面使用MVVM+Koin的架构代码 [ https://github.com/parthpatibandha/MvvmCleanKotlin ]

    改装接口

    interface MovieApiService {
        @POST(ApiConstant.API_MOVIES)
        fun getAllMovieList(
            @Query("page") page : String
        ): Deferred<FlickerImageListRS>
    }
    

    存储库

    class HomeRepository constructor(
        private val homeLocalDataSource: HomeLocalDataSource,
        private val homeRemoteDataSource: HomeRemoteDataSource
    ) : BaseRepository(), HomeRepo {
        override suspend fun getAllMovieList(flickerImageListPRQ: FlickerImageListPRQ): Either<MyAppException, FlickerImageListRS> {
            return either(homeRemoteDataSource.getAllMovieList(flickerImageListPRQ))
        }
    }
    
    

    视图模型

    class MovieListingViewModel constructor(private val homeRepo: HomeRepo) : BaseViewModel() {
    
        val movieListRSLiveData: MutableLiveData<FlickerImageListRS> = MutableLiveData()
        fun getMovieList(page : String) {
            launch {
                postValue(homeRepo.getAllMovieList(FlickerImageListPRQ(page)), movieListRSLiveData)
            }
        }
    

    希望它能帮助你理解Koin的MVVM。

    推荐文章