Skip to content

Commit

Permalink
Add sample for converting coroutine to RxJava 2 Completable
Browse files Browse the repository at this point in the history
  • Loading branch information
Elijah Verdoorn committed Sep 18, 2019
1 parent 2d95c22 commit f441eb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/kotlin/interop/completable/CompletableInterop.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
package interop.completable

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.rx2.rxCompletable
import kotlin.coroutines.CoroutineContext

class CompletableInterop(
override val coroutineContext: CoroutineContext = Dispatchers.Default
): CoroutineScope {
fun doWork() = CoroutineScope(coroutineContext).rxCompletable {
delay(1000)
println("Hello from the coroutine!")
}
}

9 changes: 9 additions & 0 deletions src/main/kotlin/interop/completable/TestCompletableInterop.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package interop.completable

fun main() {
println("Creating an instance of CompletableInterop")
val completableInterop = CompletableInterop()
completableInterop.doWork().subscribe()

Thread.sleep(2000) // Sleep the main thread to await the completion of the background thread
}

0 comments on commit f441eb1

Please sign in to comment.