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

在某个返回值上停止Kotlin流

  •  0
  • dev_android  · 技术社区  · 4 年前

    fun uploadProjectData(contactId: String, projectId: String) = flow<Resource<Boolean>> {
            emit(test1(contactId, projectId))
            emit(test2(contactId, projectId))
            emit(test3(contactId, projectId))
        }.onEach {
            if (it.errorCode != 0) {
                return@onEach
            }
            delay(0)
        }.flowOn(
            Dispatchers.IO
        )
    
        suspend fun test1(contactId: String, projectId: String): Resource<Boolean> {
            // api call or DB operation
            return responseHandler.handleSuccess(true, "test1 Successful")
        }
         
        suspend fun test2(contactId: String, projectId: String): Resource<Boolean> {
            // api call or DB operation
            return responseHandler.handleError(999, "test2 Unsuccessful")
        }
    
        suspend fun test3(contactId: String, projectId: String): Resource<Boolean> {
            // api call or DB operation
            return responseHandler.handleSuccess(true, "test2 Successful")
        }
    

    尽管test2()返回999个错误代码,但执行不会停止,它也会执行test3()。这个代码有什么问题?如何解决?

    0 回复  |  直到 4 年前