Loading...
Home
  • News
    • Android News
    • Droidcon News
  • Conferences
    • Conference Videos
    • Upcoming Conferences
    • Become a Partner
    • Past Events
  • Community
    • droidcon Team
    • Forums
    • Keep Me Informed
    • How to Hold a Droidcon
  • Jobs
    • jobs.droidcon.com
Sign In

Global CSS

 

Using Retrofit 2 with Kotlin coroutines

 

 
Zohaib A.
Android Developer ❤ open source
Published: November 16, 2019
Tweet
Share
 


Since the release of Retrofit2 version 2.6.0, there has been added native support for coroutines, this helps to achieve better performance and reduces boilerplate significantly. I haven't seen many people adapting to this change, So I hope this article helps you to adapt your app.

In this article I will be using the example of an air quality finding app that I made, the application is written in Kotlin and uses MVVM, so 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 coroutine 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


 

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 coroutine.


 

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 coroutine.

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 coroutine 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.


 

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


 

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

Thats it! your code has now become significantly cleaner and you have leveraged the power of coroutines, show some support if this article has helped you, ask me below if you have any questions.

 

Tags: Android, Android App Development, Kotlin, Kotlin Coroutines, AndroidDev

 

View original article at: 


 

 

Android News
Creating an Expandable Floating Action Button in Android — Part 2
Creating an Expandable Floating Action Button in Android — Part 2

By TJ

It’s been a little over a year since the post above, and a lot of things can change in a year. Specifically, I’ve since moved to Kotlin as my primary language for Android development, and I’ve found a cleaner and more aesthetically pleasing way to create an expandable floating action button in Android; let’s talk more about the latter.

 

By ProAndroidDev -
Android News
Phantom Types in Kotlin
Phantom Types in Kotlin

By Danny Preussler

While working on my KotlinConf presentation about types I stumbled upon this post by Maximiliano Felice about Phantom types with Scala. The idea: prevent objects from having an illegal state or forbid illegal operation at compile time. The example given was a class Door. A door is either open or closed. In Kotlin this would look like:

By ProAndroidDev -
Android News
Exploring the new CameraX library in Android
Exploring the new CameraX library in Android

By Siva Ganesh Kantamani

CameraX is a Jetpack support library which is in Alpha at present. CameraX library is build to simplify the usage of camera features in app development. CameraX provides an in-depth analysis of what camera is pointed at through Image Analysis and also provides a way to use built-in device camera features like HDR, portrait mode and so on through extensions.

By ProAndroidDev -
Android News
A fusion between WorkManager and AlarmManager
A fusion between WorkManager and AlarmManager

By Stavro Xhardha

Android’s WorkManager has been around for a while. However my own expectations about it were a little higher. I wished that the WorkManager could fire events at exact timing. But since it was made to respect doze mode, I should respect that too. That means that if the phone is idle, the WorkManager  won’t run.

By ProAndroidDev -

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2019. All rights reserved.

powered by Breakpoint One