kotlin-coroutines

Coroutines - unit testing viewModelScope.launch methods

痴心易碎 提交于 2020-06-24 08:27:45
问题 I am writing unit tests for my viewModel, but having trouble executing the tests. The runBlocking { ... } block doesn't actually wait for the code inside to finish, which is surprising to me. The test fails because result is null . Why doesn't runBlocking { ... } run the launch block inside the ViewModel in blocking fashion? I know if I convert it to a async method that returns a Deferred object, then I can get the object by calling await() , or I can return a Job and call join() . But , I'd

How to get the Context within the Android library

假如想象 提交于 2020-06-17 15:49:10
问题 Sorry if my title did not match to what my questions is. I have created a Android Library, in which I have a Room database, As there should be only one instance of Room database, I have OfflineDatabaseManager getInstance method which provides the instance to the Android project which accesses it by passing the context. I have context within the Android project and I can pass it. I want to listen to changes happening on the database table within the library so I can do something with it, I

How to inject viewModelScope for Android unit test with Kotlin coroutines?

白昼怎懂夜的黑 提交于 2020-06-17 15:47:08
问题 Questions What is the best strategy to inject viewModelScope for Android unit tests with Kotlin coroutines? When the CoroutineScope is injected into a ViewModel for unit tests, should the CoroutineDispatcher also be injected and defined using flowOn even if it is not needed in production code? flowOn is not needed in the production code in this use case as Retrofit handles the threading on Dispatchers.IO in SomeRepository.kt , and the viewModelScope returns the data on Dispathers.Main , both

Launch a number of coroutines and join them all with timeout (without cancelling)

北慕城南 提交于 2020-06-17 15:12:56
问题 I need to launch a number of jobs which will return a result. In the main code (which is not a coroutine), after launching the jobs I need to wait for them all to complete their task OR for a given timeout to expire, whichever comes first. If I exit from the wait because all the jobs completed before the timeout, that's great, I will collect their results. But if some of the jobs are taking longer that the timeout, my main function needs to wake as soon as the timeout expires, inspect which

Launch a number of coroutines and join them all with timeout (without cancelling)

心不动则不痛 提交于 2020-06-17 15:12:09
问题 I need to launch a number of jobs which will return a result. In the main code (which is not a coroutine), after launching the jobs I need to wait for them all to complete their task OR for a given timeout to expire, whichever comes first. If I exit from the wait because all the jobs completed before the timeout, that's great, I will collect their results. But if some of the jobs are taking longer that the timeout, my main function needs to wake as soon as the timeout expires, inspect which

Kotlin Channels usage difference between Send and Offer

≯℡__Kan透↙ 提交于 2020-06-17 00:07:58
问题 Channels have two functions that allow us to send events into it. Send and offer . I would like to understand better the difference between both. I have some statements I wanna check are true. Send is a suspend function. What will make my code(not the thread) wait for it to finish. So it keep running after the event inside send was complete/cancelled. OR it will be suspend only until I can queue the event/receive it? This means that, if I use send from one channel to another, the first

Perform network task in context of Dispatcher.Main

社会主义新天地 提交于 2020-06-16 17:25:23
问题 according to help, long network tasks should be performed in the context of Dispatcher.IO. But why couldn't use suspend function like get in Dispatcher.Main context? Thread itself isn't blocked, so do we expect any problem from code like: GlobalScope.launch(Dispatchers.Main) { val client = HttpClient(Android) var data: String = client.get('http://example.com') } assuming get is suspend function taking much time. Thanks. 回答1: You are right here. You can make that network request in Dispatchers

How do you split a 'hot' stream of events from a callback in Kotlin?

那年仲夏 提交于 2020-05-31 04:58:28
问题 I'm processing a hot stream of events, arriving by callback. 'Downstream' I'd like to split it into multiple streams, and process them.The events all arrive sequentially from a single thread (which I don't control, so I don't think I can use co routines here) What is the right structure to use here? I can create a Flow pretty easily, using callbackFlow and sendBlocking, but the semantics don't seem to line up, as the Flow isn't cold. What is the best way to split a flow into multiple

Test CoroutineScope infrastructure in Kotlin

冷暖自知 提交于 2020-05-17 06:45:27
问题 would someone be able to show me how to make the getMovies function in this viewModel testable? I can't get the unit tests to await the coroutines properly.. (1) I'm pretty sure I have to create a test-CoroutineScope and a normal lifeCycle-CoroutineScope, as seen in this Medium Article. (2) Once the scope definitions are made, I'm also unsure how to tell getMovies() which scope it should be using given a normal app context or a test context. enum class MovieApiStatus { LOADING, ERROR, DONE }

Obtain entity using coroutines api

梦想与她 提交于 2020-05-16 08:56:07
问题 What is the best way to use coroutines with LiveData for selecting some data from database using Room . This is My Dao class with suspended selection @Dao interface UserDao { @Query("SELECT * from user_table WHERE id =:id") suspend fun getUser(id: Long): User } Inside of View Model class I load user with viewModelScope . Does it correct way to obtain user entity ? fun load(userId: Long, block: (User?) -> Unit) = viewModelScope.launch { block(database.load(userId)) } According developer