Search code examples
androidkotlinreactivex

How to handle a subcribe() method correctly?


I've been having troubles with subscribe() method in my code (debug console's message below)

io.reactivex.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | Expected a string but was BEGIN_OBJECT at line 1 column 2 path $

and I can't figure out how to make it right, there is my part of code where it starts

private fun startSearch(query: String) {
        disposables.addAll(IMyService.searchCourse(query)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({ courses ->
                adapter = CourseAdapter(baseContext, courses)
                recycler_search.adapter = adapter
            }, {
                Toast.makeText(this, "Not found", Toast.LENGTH_LONG).show()
            }))
    }

    private fun getAllCourses() {
        disposables.addAll(IMyService.coursesList
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({ courses ->
                adapter = CourseAdapter(baseContext, courses)
                recycler_search.adapter = adapter
            }, {
                Toast.makeText(this, "Not found", Toast.LENGTH_LONG).show()
            }))
    }

and there is the full code

parameters


Solution

  • In reactive programming, passing a subscriber to an Observable should entail how to deal with three cases:

    1. onSuccess
    2. onError
    3. onFailure

    If however, you simply want to pass a subscriber which you know for sure will not have any errors or any failures and certain that it will always succeed, then simply try onSuccess or onFailure as mentioned by @EpicPandaForce. A good practice however is to always implement the three cases as you never know.