kotlin-coroutines

Why does withContext await for the completion of child coroutines

☆樱花仙子☆ 提交于 2019-12-22 05:58:44
问题 The documentation of withContext states Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result. However, the actual behavior is that it awaits on all the child coroutines as well, and doesn't necessarily return the result of the block but instead propagates any exception in the child coroutine. suspend fun main() { try { val result = withContext(coroutineContext) { launch { delay(1000L) throw Exception("launched coroutine broke

Coroutines: runBlocking vs coroutineScope

百般思念 提交于 2019-12-20 08:40:01
问题 I was reading Coroutine Basics trying to understand and learn it. There is a part there with this code: fun main() = runBlocking { // this: CoroutineScope launch { delay(200L) println("Task from runBlocking") } coroutineScope { // Creates a new coroutine scope launch { delay(900L) println("Task from nested launch") } delay(100L) println("Task from coroutine scope") // This line will be printed before nested launch } println("Coroutine scope is over") // This line is not printed until nested

How do Kotlin coroutines work internally?

为君一笑 提交于 2019-12-20 08:29:29
问题 How does Kotlin implement coroutines internally? Coroutines are said to be a "lighter version" of threads, and I understand that they use threads internally to execute coroutines. What happens when I start a coroutine using any of the builder functions? This is my understanding of running this code: GlobalScope.launch { <---- (A) val y = loadData() <---- (B) // suspend fun loadData() println(y) <---- (C) delay(1000) <---- (D) println("completed") <---- (E) } Kotlin has a pre-defined

recyclerview not showing data?

怎甘沉沦 提交于 2019-12-13 11:25:36
问题 I am developing news app I am new to MVVM and I have implemented Kotlin coroutines and Koin with ViewModel hosted RecyclerView in fragment class but data not showing only progress bar loading below my screenshot I have posted my code to gist my code on gist below my code where I have implemented recyclerview and viewmodel logic class TopHeadlinesFragment : Fragment() { private val viewModel: MainViewModel by viewModel() private lateinit var topHeadlinesAdapter: TopHeadlinesAdapter //3

Blank screen with RecyclerView No adapter attached

房东的猫 提交于 2019-12-13 03:46:38
问题 I'm tying to parse JSON in recyclerview. App compiles fine but it's outputting empty/blank screen BlogAdapter.kt class BlogAdapter(private val blogList: List<Blog>) : RecyclerView.Adapter<BlogAdapter.ViewHolder>() { override fun getItemCount()= blogList.size private var mContext: Context? = null override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { this.mContext=parent.context; return ViewHolder( LayoutInflater.from(parent.context).inflate( R.layout.character_item,

how to break from lambda passed to recursive function when provided condition met

无人久伴 提交于 2019-12-13 03:33:06
问题 I am writing a custom loop dsl and I want it's usage to look like below var counter1 = 0 var counter2 = 0 loop { counter1 += 1 println(counter1) stopIf(counter1 == 5) // loop should terminate here and not execute rest of the code if condition matches counter2 += 2 println(counter2) stopIf(counter2 == 8) // loop should terminate here and not execute rest of the code if condition matches } I have following code which does allows me to write stopIf any number of times and anywhere in the loop

Kotlin - Coroutines with loops

送分小仙女□ 提交于 2019-12-13 01:43:01
问题 So, I have a simple algorithm that follows a tree structure in this manner: Each time it moves from one node to the next, it propagates attributes of the previous node to the next node and so on, to simulate the effects that the nodes have on each other. Sometimes a node may be connected to more than one node. With my current implementation the algorithm follows each split path to the end before completing the rest of the tree: This is sub-optimal, as all the other branches have to wait for

Android instrumentation test doesn't run to end when using Room @Transaction function

匆匆过客 提交于 2019-12-12 18:04:28
问题 I'm testing with AS 3.4.1 and Emulator running Android 9. The following test won't run, when I use a Room Dao Function annotated with @Transaction in it. class RecurrenceManagerTest : DatabaseTest() { @Rule @JvmField val instantTaskExecutorRule = InstantTaskExecutorRule() var recurringEntryId: Long = -1 @Before override fun setup() { super.setup() // only initialized the db val recurringEntry = RecurringEntry( recurrence = Recurrence(DATE.toEpochMilli(), Recurrence.DAILY) ) recurringEntryId =

Kotlin suspend fun

試著忘記壹切 提交于 2019-12-12 10:40:14
问题 I have Kotlin interface interface FileSystem { suspend fun getName(path: Path): List<String> } How I can call it from Java? What is Continuation <? super List<String>> 回答1: Kotlin implements coroutines using a mix of the regular stack-based calling convention and the continuation-passing style (CPS). To achieve that, it performs a CPS transformation on all suspend fun s by adding an implicit parameter, an object you can use to continue the program from the place where the function was called.

When using kotlin coroutines, how do I unit test a function that calls a suspend function?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 08:18:03
问题 I have a class like this class SomeClass { fun someFun() { // ... Some synchronous code async { suspendfun() } } private suspend fun suspendFun() { dependency.otherFun().await() // ... other code } } I want to unit test someFun() so I wrote a unit test that looks like this: @Test fun testSomeFun() { runBlocking { someClass.someFun() } // ... verifies & asserts } But this doesn't seem to work because runBlocking doesn't actually block execution until everything inside runBlocking is done. If I