Scenario
I have a function add()which fetches data from remoteDataSource and, if successful, saves it it to localDataSource.
When I try to return the result I am getting error Type mismatch. Required: Result<Unit> Found: Result<Foo>.
I believe this is because remoteDataSource.fetch() is returning Result<Foo> and not my nested Result.success/failure()
How can I get add() to return the nested Result<Unit>?
Note I am using the Kotlin Return type.
suspend fun add(csv: String): Result<Unit> {
return try {
remoteDataSource.fetch(csv) /*<---remoteDataSource.fetch() returns Result<Foo>*/
.onSuccess { result ->
localDataSource.save(result) /*<---localDataSource.save() returns Result<Unit>*/
.onSuccess {
Result.success(Unit)
}
.onFailure {
Result.failure<Unit>(Throwable(it))
}
}
.onFailure {
Result.failure<Unit>(Throwable(it))
}
} catch (e: Exception) {
e.printStackTrace()
Result.failure(e)
}
}
Update
I have discovered the fold() function which is able to transform the result returned by onSuccess{...}. However I am not sure if this is correctly capturing throwables in both remoteDataSource and LocalDataSource. Please confirm if this is a viable solution or if another approach should be used.
override suspend fun add(csv: String): Result<Unit> {
return try {
remoteDataSource.fetch(csv)
.onSuccess { result ->
localDataSource.save(result)
}.fold(
onSuccess = { Result.success(Unit) },
onFailure = { Result.failure(Throwable()) }
)
} catch (e: Exception) {
e.printStackTrace()
Result.failure(e)
}
}