https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

fjoglar

10/20/2020, 11:31 AM
Hi all! I'm trying to call 2 consecutive `Flow`s from an android
Activity
, I'm getting:
Copy code
java.lang.IllegalStateException: ReceiveChannel.consumeAsFlow can be collected just once
The code I'm executing from is this:
Copy code
private fun getCustomer() = lifecycleScope.launch {
        isLoading = true

        // Get the user's customer code.
        GetUserDetailsUseCase().getCustomer().collect { result ->
            when (result) {
                is Result.Success -> uploadDocument(result.data)
                is Result.Error -> showPopup(result.errorMessageResId)
            }
        }
    }

    private fun uploadDocument(customer: Customer) = lifecycleScope.launch {
        this@SignUpDocumentValidationActivity.customer = customer
        // Upload document and get URL.
        val filename = "${System.currentTimeMillis()}${customer.code}"
        when (val uploadResult = UploadImageUseCase().uploadImage(
            scaleImage(),
            filename
        )) {
            is Result.Success -> {
                Logger.i(TAG, "Image $filename.jpg uploaded successfully.")
                sendDocumentAndContinue(uploadResult.data)
            }
            is Result.Error -> showPopup(uploadResult.errorMessageResId)
        }
    }

    private fun sendDocumentAndContinue(documentUrl: String) = lifecycleScope.launch {
        // Send document to backend
        UpdateDocumentUseCase().updateDocument(
            customer!!,
            DocumentType.values()[currentStep],
            documentUrl
        ).collect { result ->
            when (result) {
                is Result.Success -> {
                    deleteImage()
                    isLoading = false
                    nextStep()
                }
                is Result.Error -> showPopup(result.errorMessageResId)
            }
        }
    }
How can I get rid of this issue and call the two collect() methods secuentially? Thanks!
t

travis

10/20/2020, 5:05 PM
Have you tried
receiveAsFlow
?
3 Views