Search code examples
androidrx-javarx-java2rx-kotlin

SwitchIfEmpty not executing maybesource inside


I have some methods

Maybe<Foo> getLocalFoo()
Single<List<Foo>> getFooFromInternet()

And want to check local item and if it's empty then fetch that item from network for example.

storage
            .getLocalFoo()
            .switchIfEmpty { network.getFooFromInternet().map { it[0] }.toMaybe() }
            .subscribe({}, {})

But seens that code doesn't execute network.getBarcodeTemplates() this function.


Solution

  • Try with ( instead of { in the switchIfEmpty line:

    .switchIfEmpty ( network.getFooFromInternet().map { it[0] }.toMaybe() )