Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Using CoroutineContext to repeat failed HTTP request

 

 
Roman Kamyshnikov
Android Developer http://t.me/rkam88 
Published: January 13, 2021
Tweet
Share
 
 

 

A common scenario in Android applications is the need to repeat a request when something goes wrong, for example when there are problems with the users internet connection.

Let’s assume that we have a screen with several HTTP requests and we need to show a dialog that lets the user retry the request that failed.

If the screen had only one request — the task would be pretty straightforward: just call the method again. But if the screen has several requests that can be called at different times we need a way to identify the function that was called. Storing the last called function is a possible solution, but it’s not very flexible: if some requests don’t need to show a “Try again” dialog we must clear the variable after a success and forgetting to do this can lead to bugs.

A simple solution to the problem above can be to use CoroutineContext to save the function we need to call again later.

By default, CoroutineContext is composed of the elements Job, CoroutineDispatcher, CoroutineName and CoroutineExceptionHandler (you can read more about it in my previous article about Coroutines). But, as stated in the docs, CoroutineContext is an indexed set of Elementinstances, so we can create our own implementation of the Elementinterface, by extending the class AbstractCoroutineContextElement:

 

class RetryCallback(val callback: () -> Unit) :   AbstractCoroutineContextElement(RetryCallback) {   companion object Key : CoroutineContext.Key<RetryCallback>}

 

Now, we can just add an instance of our custom RetryCallback element to the CoroutineContext of any coroutine we start:

 

fun someFunction(someParam: List<String>) {   viewModelScope.launch(exceptionHandler      + RetryCallback { someFunction(someParam) }) {      // perform the request, update the view with the result   }}

 

And if the request fails we can can access the callback in the CoroutineExceptionHandler:

 

private val exceptionHandler = CoroutineExceptionHandler {   coroutineContext, throwable ->   val callback: (() -> Unit)? = coroutineContext[RetryCallback.Key]                                    ?.callback   // ...}

 

From the CoroutineExceptionHandler we can just pass the function to our Activity or Fragment and invoke it once the user clicks the “Try again” button.

 

 

 

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

 

View original article at: 


 

Originally published: January 16, 2020

Android News
Our Engineering Roadmap
Our Engineering Roadmap

By Mark Ng

We just completed our engineering road map for our Android apps at Australia Post. Each year we get together and try to decide on what we are going to do from an engineering perspective for the next 12 months. Each team gets to decide on what should be done now, what they want to complete by the end of the year and whats on the horizon for next year.

By ProAndroidDev -
Android News
Android Activity Lifecycle considered harmful
Android Activity Lifecycle considered harmful

By Eric Silverberg

The Android activity lifecycle is one of the first architectural concepts learned by a new Android developer. Ask any Android dev and they’ll likely all have seen this handy diagram from Google: 

By ProAndroidDev -
Android News
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App
Our Safe Approach to Android Jetpack Navigation in a Multi-Modular App

By Alejandro Weichandt

It has been a year since we started working on the Android version of the mobile app at Sync. During that year, we faced more than once that moment when we had to choose which path to follow on an Architectural decision. This story is about Navigation.

By ProAndroidDev -
Android News
Custom KotlinX Serializers
Custom KotlinX Serializers

By Jobin Lawrance

Let’s say we have a third-party class that we are using as a type in one of our data class that we want to be serialized, then we have to write a custom serializable for @Serializable to work.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

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 © 2020. All rights reserved.

powered by Breakpoint One