kotlinx.coroutines

Kotlin Android debounce

懵懂的女人 提交于 2019-12-12 07:47:33
问题 Is there any fancy way to implement debounce logic with Kotlin Android? I'm not using Rx in project. There is a way in Java, but it is too big as for me here. 回答1: You can use kotlin coroutines to achieve that. Here is an example. Be aware that coroutines are experimental at kotlin 1.1+ and it may be changed in upcoming kotlin versions. UPDATE Since Kotlin 1.3 release, coroutines are now stable. 回答2: Thanks to https://medium.com/@pro100svitlo/edittext-debounce-with-kotlin-coroutines

Coroutine unregister reciever on cancel

被刻印的时光 ゝ 提交于 2019-12-11 07:49:49
问题 My coroutine leaks a broadcast receiver, when the service is stopped. This is because the service stops, before the callback is finished. How can I cancel coroutine in a way that lets me unregister the reciever? The Service works like this: class DataCollectorService : Service(){ var job : Job? = null override fun onStartCommand(...){ job = GlobalScope.launch { val location = async { wifiScanner.getCurrentLocation() } //other asynchronous jobs location.await() //do something with location } }

Android camera2 createCaptureRequest returns all black pixels when getting YUV_420_888 image

倾然丶 夕夏残阳落幕 提交于 2019-12-11 03:15:56
问题 I have a Android camera2 API preview running ok in Kotlin using suspendCoroutine for all the surface setup and callbacks. But when I try to take a picture 5 seconds after the app starts (TEMPLATE_STILL_CAPTURE, YUV_420_888, smallest res) for some reason it all goes completely black for a moment (even in the preview window) and I get a YUV image full of 0-lum pixels. private suspend fun captureStill(): Image = suspendCoroutine { cont -> val captureRequestStill = cameraDevice

Shut down the underlying Executor of ExecutorCoroutineDispatcher

纵饮孤独 提交于 2019-12-11 01:46:09
问题 I repeatedly find myself writing code as this: val threadPoolExecutor = Executors.newCachedThreadPool() val threadPool = threadPool.asCoroutineDispatcher() What I really need is just the coroutine dispatcher so I can write stuff like launch(threadPool) { ... } or withContext(threadPool) { ... } and I need the threadPoolExecutor just to be able to shut it down on cleanup. Is there a way to use the coroutine dispatcher instance to shut it down instead? 回答1: At the moment this is no out-of-the

Kotlin coroutines: Switching context when testing an Android Presenter

半腔热情 提交于 2019-12-10 10:25:09
问题 I've started using kotlin coroutines in my Android project recently, but I have somewhat of a problem with it. Many would call it a code smell. I'm using an MVP architecture where the coroutines are started in my presenter like this: // WorklistPresenter.kt ... override fun loadWorklist() { ... launchAsync { mViewModel.getWorklist() } ... The launchAsync function is implemented this way (in my BasePresenter class that my WorklistPresenter class extends): @Synchronized protected fun

Error calling Dispatchers.setMain() in unit test

可紊 提交于 2019-12-08 17:33:41
问题 Have started to try to use kotlinx-coroutines-test (https://github.com/Kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-test/README.md) in JUnit unit test but getting following error when i call Dispatchers.setMain() java.lang.IllegalArgumentException: TestMainDispatcher is not set as main dispatcher, have Main[missing, cause=java.lang.AbstractMethodError: kotlinx.coroutines.test.internal.TestMainDispatcherFactory.createDispatcher()Lkotlinx/coroutines/MainCoroutineDispatcher;]

MongoDB reactive template transactions

为君一笑 提交于 2019-12-08 02:23:52
问题 I've been using mongodb for my open source project for more than a year now and recently I decided to try out the transactions. After writing some tests for methods that use transactions I figured out that they throw some strange exceptions and I can't figure out what is the problem. So I have a method delete that uses custom coroutine context and a mutex : open suspend fun delete(photoInfo: PhotoInfo): Boolean { return withContext(coroutineContext) { return@withContext mutex.withLock {

Mocked suspend function returns null in Mockito

不想你离开。 提交于 2019-12-07 06:04:47
问题 I have a suspending functions that I have mocked, using Mockito but it is returning null both projects use 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0' Example 1 here is my test in which the mock is returning null @Test fun `when gps not enabled observer is notified`() = runBlocking { // arrange `when`(suspendingLocationService.getCurrentLocation()).thenReturn(result) // <- when called this returns null // act presenter.onStartShopButtonClick() // assert verify(view).observer verify

Wait For Data Inside a Listener in a Coroutine

℡╲_俬逩灬. 提交于 2019-12-07 01:40:59
问题 I have a coroutine I'd like to fire up at android startup during the splash page. I'd like to wait for the data to come back before I start the next activity. What is the best way to do this? Currently our android is using experimental coroutines 0.26.0...can't change this just yet. UPDATED: We are now using the latest coroutines and no longer experimental onResume() { loadData() } fun loadData() = GlobalScope.launch { val job = GlobalScope.async { startLibraryCall() } // TODO await on

Multithreading using Kotlin Coroutines

旧时模样 提交于 2019-12-06 12:46:06
I'm experimenting with Kotlin Coroutines and have following code: fun main(args: Array<String>) = runBlocking { val cores = Runtime.getRuntime().availableProcessors() println("number of cores: $cores") val jobs = List(10) { async(CommonPool) { delay(100) println("async #$it on thread ${Thread.currentThread().name}") } } jobs.forEach { it.join() } } This is my output: number of cores: 4 async number:0 on thread ForkJoinPool.commonPool-worker-2 async number:2 on thread ForkJoinPool.commonPool-worker-3 async number:3 on thread ForkJoinPool.commonPool-worker-3 async number:4 on thread ForkJoinPool