Skip to content

Instantly share code, notes, and snippets.

View pamartineza's full-sized avatar

Pablo A Martínez Andrés pamartineza

  • GreenLionSoft Desarrollos Digitales
  • Madrid
View GitHub Profile
@pamartineza
pamartineza / PseudoCodeTestExample.kt
Created May 15, 2019 14:41
Coroutines and Testing
////////////
// Kotlin //
////////////
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.31"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1"
////////////////
// Unit Tests //
////////////////
grep -rnw '.' -e 'android.support'
@pamartineza
pamartineza / GitSubmodulesCheatsheet.txt
Last active June 28, 2023 08:41
Git Submodules Cheatsheet
//Adding a submodule:
git submodule add git@github.com:GreenLionSoft/xxxx.git folderName
git submodule init
git submodule update
cd folderName
git checkout xxxx
//Config for checking submodules before pushing
git config push.recurseSubmodules check
// Our function Zips error emissions with a range to limit them to 3,
// and emits a 0L with a power of 2 based delay
// if limit of retries is reached error is thrown to ensure it reaches
// our subscribe block
val RETRIES_LIMIT = 3
webservice.getRequestSingle()
.retryWhen { errors: Flowable<Throwable> ->
errors.zipWith(
Flowable.range(1, RETRIES_LIMIT + 1)
Bifunction<Throwable, Int, Int> { error: Throwable, retryCount: Int ->
@pamartineza
pamartineza / Medium_RetryWhen_5.kt
Last active September 8, 2018 20:39
Example of Exponential Backoff using RxJava 2 retryWhen() with interruption for UnknownHostException
// Our function Zips error emissions with a range to limit them to 3,
// and emits a 0L with a power of 2 based delay.
// If emitted error is an UnknownHostException no more retries are done and
// error is thrown, otherwise if the limit of retries is reached error is
// thrown too, in both cases to ensure it reaches our subscribe block
val RETRIES_LIMIT = 3
webservice.getRequestSingle()
.retryWhen { errors: Flowable<Throwable> ->
errors.zipWith(
Flowable.range(1, RETRIES_LIMIT + 1)
//Our function Zips error emissions with a range to limit them to 3,
// and emits a 0L with a power of 2 based delay
webservice.getRequestSingle()
.retryWhen { errors: Flowable<Throwable> ->
errors.zipWith(
Flowable.range(1,3)
BiFunction<Throwable, Int, Int> { error: Throwable, retryCount: Int ->
retryCount
}
).flatMap { retryCount: Int ->
//This observable timeline would be:
//0:00 Subscription takes place
//0:02 emits "0L" after 2 seconds
//0:04 emits "0L" after 4 seconds
//0:08 emits "0L" after 8 seconds
Observable.range(1,3).flatMap { retryCount ->
Observable.timer(Math.pow(2.toDouble(), retryCount.toDouble()).toLong(), TimeUnit.SECONDS)
}
//retryCount is a Long variable value
//eg. When retryCount = 1, observable emmits a 0L after 2 sec, then completes
//eg. When retryCount = 2, observable emmits a 0L after 4 sec, then completes
//eg. When retryCount = 3, observable emmits a 0L after 8 sec, then completes
Observable.timer(Math.pow(2.toDouble(), retryCount.toDouble()).toLong(), TimeUnit.SECONDS)
@pamartineza
pamartineza / Medium_RetryWhen_1.kt
Created September 7, 2018 10:11
Medium Gist Exponential
//It emits "1", "2", "3" then signals completion
Observable.range(1,3)
class FavoritesMemoryCache {
//Bus Stop Favorites
private var busStopFavoritesList: MutableList<BusStopFavorite>?=null
fun retrieveBusStopFavoriteList(): List<BusStopFavorite>? {
if (busStopFavoritesList != null) {
return busStopFavoritesList.toList()
}
return null