Tuesday 29 November 2022

Make API call for every item in a collection and return final results in list

I am trying to make an API call (getAccounts) that returns a list, then make a second list that by making a call(get for details of every object in that list (instead of just the first in the block below). How do I do that and then notify the observer when that series of calls is complete?

  viewModelScope.launch {
            gateway.getAccounts(

            ).fold(
                onSuccess = { v ->
                    // fetch account details for first credit account in list
                    gateway.getCreditAccountDetails(v.first().accountId, v.first().accountIdType).fold(
                        onSuccess = { v -> addAccountToList(v)}
                    )
                }
            )
        }

Coroutines functions:

   suspend fun getAccounts(): Result<List<LOCAccountInfo>> {
        return withContext(Dispatchers.IO) {
            AccountServicing.getCreditAccounts(creditAuthClient)
        }
    }

   
    suspend fun getCreditAccountDetails(accountId: AccountId, accountIdType: AccountIdType): Result<LOCAccount> {
        return getCreditAccountDetails(accountId, accountIdType, creditAuthClient)
    }


from Make API call for every item in a collection and return final results in list

No comments:

Post a Comment