kotlin-coroutines

How to create a call adapter for suspending functions in Retrofit?

让人想犯罪 __ 提交于 2019-12-12 07:14:24
问题 I need to create a retrofit call adapter which can handle such network calls: @GET("user") suspend fun getUser(): MyResponseWrapper<User> I want it to work with Kotlin Coroutines without using Deferred . I have already have a successful implementation using Deferred , which can handle methods such as: @GET("user") fun getUser(): Deferred<MyResponseWrapper<User>> But I want the ability make the function a suspending function and remove the Deferred wrapper. With suspending functions, Retrofit

Async not waiting for await

痴心易碎 提交于 2019-12-12 05:48:36
问题 I'm new to Kotlin and the coroutines. However I want to use it to initialize the Android ThreeTen backport library which is a long running task. I'm using the Metalab Async/Await Library (co.metalab.asyncawait:asyncawait:1.0.0). This is my code: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val application = this async { //non-blocking initialize ThreeTen await { AndroidThreeTen.init(application) } //initialize

Use main thread after long network operation

巧了我就是萌 提交于 2019-12-11 17:08:43
问题 In my android app I has long network operation. After operation is finished I need to update my ui. So as result long operation need to execute in background thread. Snippet: private val isShowProgressLiveData = MutableLiveData<Boolean>() // launch a new coroutine in background and continue GlobalScope.launch() { try { val executeOperations = OperationFactory.createExecuteTraderOperation(Trader.Operation.CREATE, base, quote) val response: Response<Void> = executeOperations.await() if

Kotlin Json parsing MVVM

故事扮演 提交于 2019-12-11 16:59:38
问题 I'm trying to learn MVVM architecture on parse Json into a Recyclerview in MVVM using coroutines. But I'm getting error on BlogRepository class. My Json file looks like this: [ { "id": 1, "name": "potter", "img": "https://images.example.com/potter.jpg" }, { …} ] I have created data class as below: @JsonClass(generateAdapter = true) class ABCCharacters ( @Json(name = "id") val char_id: Int, @Json(name = "name") val name: String? = null, @Json(name = "img") val img: String ) Then the

How to write unit tests for kotlin coroutines

六月ゝ 毕业季﹏ 提交于 2019-12-11 16:54:23
问题 I am trying to unit test the kotlin coroutines. My project is following MVP pattern where the coroutines are used in the presenter like this: fun authenticateWithUserAndPassword(usernameOrEmail: String, password: String) { launchUI(strategy) { view.showLoading() try { val token = retryIO("login") { when { settings.isLdapAuthenticationEnabled() -> client.loginWithLdap(usernameOrEmail, password) usernameOrEmail.isEmail() -> client.loginWithEmail(usernameOrEmail, password) else -> client.login

What is the order of execution with coroutines?

元气小坏坏 提交于 2019-12-11 16:48:18
问题 Consider the following code in kotlin. val scope = CoroutineScope(Dispatchers.Main + Job()) scope.launch { println("inside coroutine") } println("outside coroutine") We create a coroutine in the Main(UI) thread and there is some code after the coroutine. I know it doesn´t make much sense to do that in real code, but it´s just a theoretical question. Considering that the coroutine runs in the Main thread, why println("outside coroutine") is ALWAYS executed first? I would have expected that

Kotlin coroutines, continues updates

柔情痞子 提交于 2019-12-11 15:26:49
问题 New to kotlin, i tried many examples and tutorials to no avail, My requirement is: Ui creates a coroutine that initiates a network connection on press of a button, that coroutine sends a msg like "i need info about foo" (taken from a edittext?) to the connected server. coroutine should also be listening for incoming messages and pass those messages to ui (or update ui directly) coroutine should keep connected to the server unless it is told to close the connection. I feel that i need global

How would I “wrap” this not-quite-“by lazy” result caching function call in idiomatic Kotlin?

ε祈祈猫儿з 提交于 2019-12-11 12:13:03
问题 I can't use "by lazy" because the callbacks require suspendCoroutine , which borks in android if it blocks the main thread, so I have to use the following "cache the result" pattern over and over. Is there a way to wrap it in a funButUseCachedResultsIfTheyAlreadyExist pattern to encapsulate the xCached object? private var cameraDeviceCached: CameraDevice? = null private suspend fun cameraDevice(): CameraDevice { cameraDeviceCached?.also { return it } return suspendCoroutine { cont:

Execute a piece of code when Kotlin channel becomes full

橙三吉。 提交于 2019-12-11 11:55:35
问题 I'd like to write a simple batch processor class. It has a request queue and waits this queue to become full or some amount of time to be passed and only then talks to a database. It is very convenient to implement this queue via channel - so that our clients will be suspended while it is full. But how can I find out if the channel becomes full? Of course I can create a method that sends something to the channel and then performs some checks. The next step is to encapculate it in a class

IllegalArgumentException: Parameter specified as non-null is null: method kotlinx.coroutines.BuildersKt__Builders_commonKt.launch, parameter context

风流意气都作罢 提交于 2019-12-11 06:36:21
问题 I am trying to unit test the below function which uses kotlin coroutines fun authenticateWithUserAndPassword(usernameOrEmail: String, password: String) { launchUI(strategy) { //line 76 view.showLoading() try { val token = retryIO("login") { when { settings.isLdapAuthenticationEnabled() -> client.loginWithLdap(usernameOrEmail, password) usernameOrEmail.isEmail() -> client.loginWithEmail(usernameOrEmail, password) else -> client.login(usernameOrEmail, password) } } val myself = retryIO("me()")