2
votes

Re-edit : I will use the result of this method to initialize the visibility of some buttons in my view The accepted answer seems good in theory but the return value of first method is Flowable> instead of Flowable. Hence I cannot pass it as a parameter to subscription since it requires Flowable but it is Flowable>

Question Before Edit

I am using RxJava to observe a method I am required to call from SDK. Using this method I am trying to make an assertion about the existence of something but I do not know how long the call will take so it is hard for me to say terminate the subscription after x seconds.

override fun doesExist(): Boolean {
        var doesExist = false
        var subscription : Subscription
        val flowable = Flowable.just(SDK.searchContact("contact"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : FlowableSubscriber<Flowable<List<Contact>>> {

                    override fun onSubscribe(s: Subscription) {
                        subscription = s
                    }

                    override fun onNext(t: Flowable<List<Contact>>?) {
                        doesExist = true
                    }

                    override fun onComplete() {
                        Log.d("tagtagcomplete", "tagtagcomplete")

                    }

                    override fun onError(e: Throwable?) {
                        Log.d("tagtagerror", "tagtagerror")
                    }


        return doesExist
    }

So what I want to do is return true if I won't get any result from searchContract method and return false if I get a result. While using observables, I can create a subscription object and call it's methods but I could not figure out how to do it right way with flowables.

My confusion is the following: Method to sdk returns a Flowable<List<Contact>>> but in my opinion I need to check if a contact exists only once and stop

Right now my method does not go inside onError, onNext or onComplete. It just returns doesExist

1

1 Answers

1
votes

I reedit my answer,following return type is you need.

fun doesExist(): Flowable<Single<Boolean>> {
    return Flowable.just(Single.just(SDK.searchContact("contact")).map{ it.isEmpty()})
}