Skip to content

Instantly share code, notes, and snippets.

@sliskiCode
Last active March 24, 2018 13:14
Show Gist options
  • Save sliskiCode/6b64821ee91d71cc2c5825347916164b to your computer and use it in GitHub Desktop.
Save sliskiCode/6b64821ee91d71cc2c5825347916164b to your computer and use it in GitHub Desktop.
RxJava Quiz - Use case: I want to delay emissions of server response until transition animation ends, but the request should happen right away when screens starts opening
buildscript {
ext.kotlin_version = '1.0.6'
ext.rx_version = '2.0.4'
ext.retrofit_version = '2.1.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}
apply plugin: "kotlin"
apply plugin: "java"
sourceSets {
main {
java {
srcDirs = ['src/kotlin']
}
}
}
dependencies {
compile "io.reactivex.rxjava2:rxjava:$rx_version"
compile "com.squareup.retrofit2:retrofit:$retrofit_version"
compile "com.squareup.retrofit2:converter-gson:$retrofit_version"
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.ReplaySubject
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
val api: StackoverflowApi = Retrofit.Builder()
.baseUrl("https://api.stackexchange.com/2.2/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(StackoverflowApi::class.java)
/**
* This solution is good for network calls because they are always Singles.
* Using hot Observables with cache can lead to OOMs.
*/
fun main(args: Array<String>) {
val single = api.getPosts(1408086L)
println("Open screen")
single.subscribeOn(Schedulers.newThread()).subscribe{ it -> println("DONE!") } // Starting network call
println("Animation start: 10s")
Thread.sleep(10000)
println("Animation end with subscription")
single.observeOn(Schedulers.io()).subscribe { it -> it.posts.forEach(::println) }
}
/**
* Solution with source and delayer.
*/
fun main2(args: Array<String>) {
val delayer = ReplaySubject.create<List<Post>>()
val source = api.getPosts(1408086L)
println("Open screen")
source.subscribeOn(Schedulers.newThread()).subscribe { it -> delayer.onNext(it.posts) } // Starting network call
println("Animation start: 10s")
Thread.sleep(10000)
println("Animation end with subscription")
delayer.subscribe { it -> it.forEach(::println) }
}
import com.google.gson.annotations.SerializedName
class PostWrapper<out Post>(@SerializedName("items") val posts: List<Post>)
data class Post constructor(@SerializedName("post_id") val postId: Long,
@SerializedName("score") val score: Int,
@SerializedName("link") val link: String)
import com.slesarew.model.Post
import com.slesarew.model.PostWrapper
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.Path
interface StackoverflowApi {
@GET("users/{userId}/posts?site=stackoverflow")
fun getPosts(@Path("userId") userId: Long): Single<PostWrapper<Post>>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment