Beginning RxKotlin 路 Introduction | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/7419-beginning-rxkotlin/lessons/1

Hi.

I managed to install Rx with the help of comments, but could you please update the sample code?

Do you have any plan for RxJava3?

Hi @yutarokonda and thanks for the question! We hope to update the course for RxJava 3 in the near-to-mid term. Are you using RxJava 2, and if so, did you run into any specific problems with the sample code? Thanks again!

Thank you! I鈥檓 looking forward to the update while continuing with RxJava2.

I had difficulty setting up Intellij Idea (JDK and gradle), as I only use Android Studio at work.
Now that it鈥檚 properly set up, I can run codes from any lesson smoothly.

I have a question from lesson 6. How do you check if memory is leaked? My terminal didn鈥檛 have println outputs from the observer and the process just finished without any problem.

Thanks @yutarokonda! Could you paste in here the code you entered so we can try to diagnose the issue?

I tried it now, and I see printed text. There must鈥檝e been some mistake in my previous code. Sorry about the confusion.

About memory leakage, should Observable have either onError or onComplete event defined if you don鈥檛 use CompositeDisposable? Can you be loose about it when you use CompositeDisposable?
I don鈥檛 see any sign of memory leaking. Is there any way of detecting it?

Below is the code I just tried (should be exactly the same from lesson 6)

import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.subscribeBy

sealed class Droid: Throwable() {
    class OU812: Droid()
}

sealed class FileReadError: Throwable() {
    class FileNotFound: FileReadError()
}

fun main(args: Array<String>) {

    exampleOf("create") {

        val subscriptions = CompositeDisposable()

        val droids = Observable.create<String> {emitter ->
            emitter.onNext("R2-D2")
//            emitter.onError(Droid.OU812())
            emitter.onNext("C-3PO")
            emitter.onNext("R-2SO")
        }

        val observer = droids.subscribeBy(
                onNext = { println(it)},
                onError = { println("Error, $it")},
                onComplete = { println("Completed")}
        )

//        subscriptions.add(observer)
    }
}

Generally, you should dispose of subscriptions one way or another. Using onError and onComplete is independent of disposing subscriptions. onError is generally a good idea. onComplete depends on your use case.

With respect to detecting leaks, LeakCanary is a good choice.

Thank you. I鈥檒l ask you again when I get a new question.