Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

liveData with Coroutines #715

Closed
isfaaghyth opened this issue Sep 6, 2019 · 6 comments
Closed

liveData with Coroutines #715

isfaaghyth opened this issue Sep 6, 2019 · 6 comments

Comments

@isfaaghyth
Copy link

isfaaghyth commented Sep 6, 2019

Hi! who already use for this one androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01 ? I've tried it into my repository layer but doesn't work (i'm using retrofit 2.6.0). this is my code:

interface Service {
     @GET("/data")
     suspend fun getData(): Data
}
class Repository @Inject constructor(val service: Service) {
     fun getData(): LiveData<Data> = liveData {
          val data = service.getData()
          emit(data)
     }
}

and this is my viewmodel:

...
     fun fetch() {
          viewModelScope.launch {
              val data = repository.getData()
              println(data.value)
          }
     }
...

the result is null.

any idea about this?
thanks!

@alexkazancew
Copy link

alexkazancew commented Sep 6, 2019

Hi @isfaaghyth . Your liveData from repository.getData() don`t have active observers. So liveData not emiting data. Try something this.

...
     fun fetch():LiveData<Data> {
         return repository.getData(viewModelScope)
          }
     }
...
class Repository @Inject constructor(val service: Service) {
     fun getData(scope:CoroutineScope): LiveData<Data> = liveData(scope.coroutineContext) {
          val data = service.getData()
          emit(data)
     }
}
 class SomeFragment:Fragmet(){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { ...
        viewModel.fetch().observe(viewLifecycleOwner, Observer{
                 println(it)
      }
  }
}

P.S.
Correct me if something is wrong

@isfaaghyth
Copy link
Author

so, which a good way for my case? @alexkazancew

@alexkazancew
Copy link

@isfaaghyth i update my previous answer

@isfaaghyth
Copy link
Author

it works! thanks @alexkazancew

@GuilhE
Copy link

GuilhE commented Sep 24, 2019

How about usingliveData{... emit()}while using SavedStateHandle? Is there any benefit?
Here's my SO question

@alexkazancew
Copy link

@GuilhE I think you can do something like this

class SomeViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() {

    companion object {
        private const val PAGE_KEY = "page_key"
    }

    private val _page = MutableLiveData<PageId>(savedStateHandle.get(PAGE_KEY))

    private val _itemLiveData = Transformations.switchMap(_page) { pageId -> repository.getNextPage(pageId) }
    val itemLiveData: LiveData<MyItem> = _itemLiveData

    suspend fun nextPage(pageId: PageId) {
        _page.postValue(pageId)
    }

    override fun onCleared() {
        super.onCleared()
        savedStateHandle.set(PAGE_KEY, _page.value)
    }
}

Let me know if it helped you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants