所以我真的不喜欢Call::enqueue
的回调方面。
我宁愿将对Call::execute
的调用包装到Future
中。
做这样的事有多安全
/**An auxiliary retrofit controller to facilitate communication with the server during testing.*/
object Server : CoroutineScope{
private val job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.IO
const val URL = ...
private val retrofit = Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(DebugServerAPI::class.java)
/**Sends the [somethingRequest] to the server.
* @return a [Deferred] holding the server [Response]*/
fun trySomething(jwt:String,somethingRequest:JsonObject):Deferred<Response<JsonObject>>{
return async { retrofit.something(jwt,somethingRequest).execute() }
}
}
?