Kashif
02/03/2020, 4:06 PMoverride fun getOrderInfoList(status: String, chainID: String, debugTag: String): Flowable<Result<List<OrderInfo>>> {
logger.d(TAG, "Getting order list for status %s", status)
val fetchFromStorageObservable = storageDataSource.getOrderList(debugTag)
val observeStorageObservable = storageDataSource.observeOrderList(debugTag)
// function which fetches from the local storage and takes a throwable that might be re-thrown if there's no data found locally
val fetchFromStorageOrThrowObservable = fun (t : Throwable) : Observable<Result<List<OrderInfo>>> {
fetchFromStorageObservable.map {
if(it.isEmpty()) {
// re-throw the exception from the Remote source since we got no data from the Local source
throw t
}
it
}
}
val fetchFromRemoteObservable = remoteDataSource.getOrderInfoList(status, chainID).toObservable().onErrorResumeNext { t: Throwable ->
logger.d(TAG, "order list not received from server")
// fallback to the local source
fetchFromStorageOrThrowObservable(t)
}.flatMap {
if (it is Result.Success) {
persisOrderInfoList(it.data)
}
fetchFromStorageObservable
}
return Observable.concat(fetchFromRemoteObservable, observeStorageObservable).toFlowable(BackpressureStrategy.LATEST)
}
Kroppeb
02/03/2020, 8:09 PMit
in the fetchFromStorageObservable.map
. Is it
the list?fetchFromStorageObservable
?fetchFromStorageOrThrowObservable
doesn't have a return statement?Result.Success
doesn't exist. Is this a self-defined type?Kashif
02/04/2020, 6:14 AMit
in the fetchFromStorageObservable.map
. Is it
the list? ”
yes it is List<OrderInfo>
“what type is `fetchFromStorageObservable`”?
Observable
“Also the function for fetchFromStorageOrThrowObservable
doesn’t have a return statement”?
true, my bad, forgot to add return statement
"Result.Success
doesn’t exist. Is this a self-defined type”?
yes it is self-defined type which is like:
sealed class Result<out T : Any> {
data class Success<out T : Any>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
override fun toString(): String {
return when (this) {
is Success<*> -> "Success[data=$data]"
is Error -> "Error[exception=$exception]"
}
}
}
Matteo Mirk
02/04/2020, 8:36 AMResult
?
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/index.htmlKroppeb
02/04/2020, 8:45 AMgetOrderInfoList
returns a Result
I think, which is not possible with a Kotlin Result
.map
is an extension-function defined on Observable<Result<T>>
?Kashif
02/04/2020, 12:15 PM