Created
February 17, 2018 19:44
-
-
Save danielgomezrico/874cd2f7a923173415817136d761d3e8 to your computer and use it in GitHub Desktop.
[Kotlin, rxjava and room] - how to always get a value from Maybe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Optional<out M>(val data: M?) | |
@Dao | |
interface PatientDao { | |
@Query("SELECT * FROM $patientTableName LIMIT 1") | |
fun getFirst(): Maybe<User> | |
} | |
val loggedInUser: Single<Optional<User>> | |
get() = userDao.getFirst() | |
.toSingle() | |
.map { Optional(it) } | |
.onErrorResumeNext { Single.just(Optional(null)) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment