无限循环收流量正常吗

问题描述 投票:0回答:0

我有如下功能,我需要向一些API发送请求。 是正确的认识吗? 来自 API 的数据包装到 kotlin Flow 中,然后在这个函数中我收集它。

private fun fetchOrder() {
    serviceScope.launch(Dispatchers.IO) {
        while (isGettingOrders) {
            delay(1000)
            getOrderInfoUseCase(outletId)
                .collect { result ->
                    result.onSuccess {
                        if (currentOrderId != it.orderId) {
                            currentOrderId = it.orderId
                            _order.emit(it)
                        }
                    }
                    result.onFailure {
                        printLog("${it.message}")
                    }
                }
        }
    }
}

我担心这个功能会启动无限流量

更新

fun getOrderInfoUseCase(outletId: Int): Flow<Result<OrderInfo>> =
    receiptRemoteRepository.getOrderInfo(outletId).map {
        Result.success(it)
    }.catch {
        Result.failure<Throwable>(it)
    }
kotlin kotlin-coroutines flow
© www.soinside.com 2019 - 2024. All rights reserved.