We are building a public SDK for our product. It is built with Kotlin and internally we use coroutines. However, we want to publish an API that is usable form JAVA, that's why can't provide suspendable functions as public API.
We are ok, if the usability in Java will be less comfortable than in Kotlin, that's quite expected.
So, for example, we are looking of a return type of the following async method:
class Sdk {
fun getPlace(): ___
}
Things we have considered:
Using a RX Java as a the interface. We don't like this solutions, Rx is quite complex and we want to add as little as possible of other dependencies. Generally, we would go with returning Single. But, Rx java things we don't want to solve (which thread should be the work done) and Rx don't solve things we would like to solve (if possible), e.g. lifecycle & observer - the things solved in Android architecture components.
Java 8 Future. This seems to be most appropriate but not possible since we need to target older Androids (4.1 at least).
Android architecture LiveData. Returning a LiveData seems to be ok, there is also a observeForever() method that makes it usable in backgrounds threads. On the other hand, the api suggests that it may return repeatadly multiple results. But we want definitely omit only on result or one exception. In Kotlin, though, we may implement extension functions which will make it quite usable as
sdk.getPlace().await()
.Custom solution: return a simple Result object, you may subsrcibe for the result by providing a callback
sdk.getPlace().observe(Observe { onSucccess(data: Place) {} onFailure(e: Throwable) {} })
; we will provide an extension function for awaiting.
The questions:
- Did we miss some important aspect/library/possibility?
- Which solution we should choose and why.