https://kotlinlang.org logo
#coroutines
Title
# coroutines
g

gmaciel

09/25/2019, 8:28 AM
Hello, can anyone answer me something about
kotlinx.coroutines.rx2
? I'm wondering if i use
rxSingle
, do i really need a context?
Copy code
* Creates cold [single][Single] that will run a given [block] in a coroutine.
 * Every time the returned observable is subscribed, it starts a new coroutine.
 * Coroutine returns a single value. Unsubscribing cancels running coroutine.
Feels like as long i subscribes and unsubscribe in the right times it takes care of the coroutine and it doesn't cause memory leaks. Like this:
Copy code
private fun single() = rxSingle {
    suspendedFun()
}
Is this correct to assume? I also can't provide a Context with a job to cancel it anyways.
g

gildor

09/25/2019, 8:34 AM
No need to provide Context
Lifecycle of this single should be managed by subscriber of this Single
Before 1.3 rxSingle was an extension of coroutine scope, but it was removed, now it’s just top-level function and only subscriber of this single manage lifecycle of it, there is no parent scope (and parent job)
👍 1
g

gmaciel

09/25/2019, 8:55 AM
Yeah exactly, i used before but now i was a bit unsure. Thanks for the answer.
2 Views