2

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 Answer 1

1

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()})
}
Sign up to request clarification or add additional context in comments.

10 Comments

Couple of questions - 1) Does this wait for searchContact to search one time. 2)When should I cancel the subscription, or does it cancel itself after some point? I can't cancel the subscription at onComplete as it warns me about the initilization of subscription
you can cancel it in other place,no in doesExist method
So how can I make it wait? I was using blockingSubscribe for observables in the previous construct. By the way this one liner seems much nicer and more understandable. I will accept it as the correct answer
do other operation after search
you can post more code ,I don't know what you want to do after searched result.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.