Sundeep Badhotiya
Sundeep Badhotiya

Reputation: 868

How to get return from Single in RxJava

I want to return true or false from Single.fromCallable{} in RxJava.Is there any other way to achieve it

private isBlockListAvailable():Boolean
{

Single.fromCallable {
        //** data base query
        blockRepo.getAllBlocks()
    }.subscribeOn(Schedulers.io())
            .subscribe({blockList : List<BlockList>
                if (blockList> 0) {
                    return true // how can i achieve this
                } else {
                   return false // how can i achieve this
                }
            })
}

Thanks in Advance:)

Upvotes: 0

Views: 2144

Answers (1)

Lan Nguyen
Lan Nguyen

Reputation: 795

No, you cant. It's impossible to return value in the subscribe method. Look detail at its implementation.

Single.subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                })

You can see the method accept of the Consummer class is a type of void. Obviously, it cant return value.

Another way, you can create Single.fromCallable if you prefer to manipulate boolean in subscribe.

Upvotes: 1

Related Questions