在继续 null 问题之前结束 flow/coroutines 任务

End flow/coroutines task before go further null issue

片段

   private fun makeApiRequest() {
        vm.getRandomPicture()
        var pictureElement = vm.setRandomPicture()

        GlobalScope.launch(Dispatchers.Main) {
            // what about internet
            if (pictureElement != null && pictureElement!!.fileSizeBytes!! < 400000) {

                Glide.with(requireContext()).load(pictureElement!!.url)
                    .into(layout.ivRandomPicture)
                layout.ivRandomPicture.visibility = View.VISIBLE
            } else {
                getRandomPicture()
            }
        }
    }

视图模型

    fun getRandomPicture() {
        viewModelScope.launch {
            getRandomPictureItemUseCase.build(Unit).collect {
                pictureElement.value = it
                Log.d("inspirationquotes", "VIEWMODEL $pictureElement")
                Log.d("inspirationquotes", "VIEWMODEL VALUE ${pictureElement.value}")
            }
        }
    }

    fun setRandomPicture(): InspirationQuotesDetailsResponse? {
            return pictureElement.value
    }

流用例

class GetRandomPictureItemUseCase @Inject constructor(private val api: InspirationQuotesApi): BaseFlowUseCase<Unit, InspirationQuotesDetailsResponse>() {
    override fun create(params: Unit): Flow<InspirationQuotesDetailsResponse> {
        return flow{
            emit(api.getRandomPicture())
        }
    }
}

我在视图模型中的流程任务没有按时进行。我不知道如何实现从Api顺利下载数据并进一步提供。 我在读我可以使用 runBlocking,但也不建议在生产中使用它。 您在专业应用中使用什么来实现漂亮的应用程序?

现在的效果是那个图像没有加载或者我有空错误,因为我在 Fragment 中的 GlobalScope 之前 Log.d(它现在不在代码中)。

还有一件事是定义空对象我不喜欢它,你怎么看?

    var pictureElement = MutableStateFlow<InspirationQuotesDetailsResponse?>(null)

编辑:

视图模型

 val randomPicture: Flow<InspirationQuotesDetailsResponse> = getRandomPictureItemUseCase.build(Unit)

片段

private fun makeApiRequest() = lifecycleScope.launch {
            vm.randomPicture
                .flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
                .collect { response ->
                    if (response.fileSizeBytes < 600000) {
                        Log.d("fragment", "itGetsValue")
                        Glide.with(requireContext()).load(response.url)
                            .into(layout.ivRandomPicture)
                        layout.ivRandomPicture.visibility = View.VISIBLE
                    } else {
                        onFloatingActionClick()
                    }
                }
    }

Edit2 生产问题,另一个话题: Link ->

首先,不要使用 GlobalScope 来启动协程,这是非常不鼓励的,而且容易出现错误。在 Fragment:

中使用提供的 lifecycleScope
lifecycleScope.launch {...}

使用MutableSharedFlow代替MutableStateFlowMutableSharedFlow不需要初始值,可以去掉可空泛型:

val pictureElement = MutableSharedFlow<InspirationQuotesDetailsResponse>()

但我想我们可以摆脱它。

方法create() in GetRandomPictureItemUseCase returns一个只发出一个值的Flow,真的需要Flow,还是可以只是一个简单的 suspend 函数?

假设我们在 GetRandomPictureItemUseCase class 中坚持使用 FlowViewModel 可能如下所示:

val randomPicture: Flow<InspirationQuotesDetailsResponse> = getRandomPictureItemUseCase.build(Unit)

并且在 Fragment 中:

private fun makeApiRequest() = lifecycleScope.launch {
    vm.randomPicture
      .flowWithLifecycle(lifecycle, State.STARTED)
      .collect { response ->
          // .. use response
      }
}

要使用的依赖性 lifecycleScope:

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'