hadiyarajesh / flower

Flower simplifies networking and database caching on Android/Multiplatform.
Apache License 2.0
288 stars 27 forks source link

fetch remote from background/service #9

Closed andrea-liu87 closed 2 years ago

andrea-liu87 commented 2 years ago

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

hadiyarajesh commented 2 years ago

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.

andrea-liu87 commented 2 years ago

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)
hadiyarajesh commented 2 years ago

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.

hadiyarajesh commented 2 years ago

Did Kotlin flow solved your issue?

hadiyarajesh commented 2 years ago

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