hadiyarajesh/flower

fetch remote from background/service

Closed this issue · 5 comments

Hello. Thank you for the cool library. Is there any way I can fetch remote data from server from BackgroundService or workerManager?

You can use it to fetch data from anywhere. Have you experienced any problem using it with service or work manager? If so, kindly provide some logs.

For now because I use it as live data, so it only can be used with viewModel which only can be called from Fragment/Activity.

Can you explain how I can called it from Background Service or Work Manager?

val someVariable: LiveData<Resource<YourModelClass>> = repository.getSomething().map {
    when (it.status) {
        Resource.Status.LOADING -> {
            Resource.loading(null)
        }
        Resource.Status.SUCCESS -> {
            Resource.success(it.data)
        }
        Resource.Status.ERROR -> {
            Resource.error(it.message!!, null)
        }
    }
}.asLiveData(viewModelScope.coroutineContext)

If you're using kotlin, you can use Flow/StateFlow in your service and collect/assign value there as it just emits Flow from api call.

Did Kotlin flow solved your issue?

I'm closing this issue with hope that Kotlin flow solved your issue. Kindly create new issue if you need further assistance.