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

 

Backoff and Retry Strategy using Flows in Android

 

 
Shivam Dhuria
Published: December 22, 2020
Tweet
Share
 

 

You might come across a situation while developing an app where you’d want to retry a certain request when an Exception occurs without user’s intervention.

A pragmatic example would be a case of an IO Exception. Consider this use case. You want to fetch a list of dogs from the server, however, you get an IO exception due to a network issue.

Now you can either trigger another request right away or wait for sometime (backoff) and retry. In this article we will explore the backoff method.

 

Image for post

In this case, The first the network request return IOException first 2 attempts and in the third attempt Result.Success is returned.

 

Setting up ViewModel

getDogImages() is a function that throw an IOException randomly, mocking a network response. In a real app it should trigger a network request.

 

 

Now this is where the magic happens. We use a special operators called .retryWhen which has two parameters cause and attempt . cause is a throwable (returned from getDogImages() flow) and attempt is the number of attempt number.

Now whenever getDogImages() throws an exception, We check if the Exception was an IOException and check if the attempt number is less than three.

If both conditions are satisfied, We emit a ResultWrapper.NetworkError(..)
to notify the UI about the Error and backoff for a time delay of 2000 * (attempt + 1) milliseconds. With each new attempt we backoff for more time than last time when retrying.

If the getDogImages() function throws Exception three times , We stop retrying and an emit ResultWrapper.NetworkError(“Retries Expired!”, null) , followed by return@retryWhen false.

Make sure you add a catch {} block to handle the above Exceptions

 

 

Setting up the View

Now in the View, collect the above flow an update UI accordingly.

 

 

And That’s It ✅

You have successfully implemented a Back Off Strategy with retry using flows. You can extrapolate this idea to any other Exception where you’d want to retry a request without user’s intervention smartly.

 

Shivamdhuria/stateFlows

An Android Application written purely in Kotlin exploring various usages of Flows and Coroutines in a Real World App…

github.com

 

 

 

 

Tags: Flows, Kotlin, Reactive, Android, Android App Development

 

View original article at: 


 

Originally published: December 13, 2020

Android News
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

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