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

Documentation request for coroutines and Retrofit #3263

Open
eothein opened this issue Dec 4, 2019 · 1 comment
Open

Documentation request for coroutines and Retrofit #3263

eothein opened this issue Dec 4, 2019 · 1 comment

Comments

@eothein
Copy link

@eothein eothein commented Dec 4, 2019

  • Documentation:

It would be nice if the official documentation explains how to use Retrofit together with co-routines together with the best practices regarding the asynchronous handling of requests. My students need to implement their calls using Retrofit and co-routines (following the tutorials in the Udacity course, but for documentation I'm relying on (not always the best) medium articles instead of official docs.

Kind regards

Jens

@GypsyTheDj
Copy link

@GypsyTheDj GypsyTheDj commented Feb 6, 2020

Since the release of Retrofit2 version 2.6.0, there has been added native support for co-routines, this helps to achieve better performance and reduces boilerplate significantly. I will show you how to change/implement your repository, view model and the UI controlling activity. Finally I will show you how to handle errors with this new approach.

The main changes that you will need to make is making the co-routine calling functions suspendable, as is required for them to function in their intended manner, i.e by stopping and starting according to the application state.

So in our API interface we make the @get function a suspend function as shown

<script src="https://gist.github.com/zedlabs/8cbe9d54dad5d57355d110f7f0ad48d0.js"></script>

Next comes the Repository class which creates the instance of the retrofit service and initiates the API call, our method for this call should be a suspend function as it will later be called through a co-routine.

<script src="https://gist.github.com/zedlabs/520e81b66d79557b138c80350606c095.js"></script>

As you can see the code which was earlier 50–60 lines has been reduced to a very clean single line, as the procedure has been streamlined through the use of a co-routine. The main thing missing from the getData() method is the error handling callback that is automatically created when a call is enqueued.This error can be handled by wrapping the co-routine in a try/catch block. Now that our repository is set up we can create a method in the viewModel which uses the liveData builder to create a process on the IO thread.

<script src="https://gist.github.com/zedlabs/c9736211e839550ac07936cb3ec96c47.js"></script>

Add this viewModel and liveData dependencies for the coroutine to work

  • implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
  • implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
  • implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"

The emit function emits the changes in the underlying database to the observer variables, in our case which will be located in the MainActivity.

<script src="https://gist.github.com/zedlabs/d1f0ae241c563f3f9d1449735d4328bd.js"></script>

Here we can see the observer observing the value of aqi from the API and updating the UI accordingly.

That's it! your code has now become significantly cleaner and you have leveraged the power of co-routines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.