kotlin-coroutines

Android Kotlin Room Repository unable to retrieve row from within detail activity

梦想的初衷 提交于 2021-02-20 01:00:08
问题 I'm really struggling with this & would appreciate some help please. I'm learning Android Kotlin & building an app that displays a list of walking routes (downloaded from the cloud) in a RecyclerView &, when a route is selected I want to display all details of the route - a simple Master-Detail app. Since, I'm learning I also want to try and use best practice. I have most of it working fine using a Room database & a Repository. The database is correctly populated and the RecyclerView displays

Using Kotlin Coroutines to replace LocalBroadcastManager for Firebase messaging

£可爱£侵袭症+ 提交于 2021-02-17 19:12:53
问题 When using Firebase Cloud Messaging on Android, it is often desirable to notify the current Activity of an incoming push notification. One of the recommended ways to do this has been to use LocalBroadcastManager to send an Intent from the FirebaseMessagingService implementation to the Activity (StackOverflow example answer). However, as of version 1.1.0-alpha01 (2018-12-17), LocalBroadcastManager is deprecated: LocalBroadcastManager is an application-wide event bus and embraces layer

How to spot where “Job was cancelled” exception comes from when all your coroutines are already wrapped with a CouroutineExceptionHandler?

六眼飞鱼酱① 提交于 2021-02-08 21:57:49
问题 I read all the kotlinx UI docs and implement a ScopedActivity like described there (see the code below). In my ScopedActivity implementation, I also add a CouroutineExceptionHandler and despite that I pass my exception handler to all my coroutines, my users are experiencing crashes and the only info I get in the stacktrace is "Job was cancelled". I searched for a couple of days now but I did not find a solution and my users are still randomly crashing but I do not understand why... Here is my

How to replace LiveData with Flow

懵懂的女人 提交于 2021-02-08 09:15:36
问题 I've one LiveData named sortOrder and then I've another variable named myData that observes any change to sortOrder and populates data accordingly. class TestViewModel @ViewModelInject constructor() : ViewModel() { private val sortOrder = MutableLiveData<String>() val myData = sortOrder.map { Timber.d("Sort order changed to $it") "Sort order is $it" } init { sortOrder.value = "year" } } Observing in Activity class TestActivity : AppCompatActivity() { private val viewModel: TestViewModel by

How to replace LiveData with Flow

↘锁芯ラ 提交于 2021-02-08 09:13:31
问题 I've one LiveData named sortOrder and then I've another variable named myData that observes any change to sortOrder and populates data accordingly. class TestViewModel @ViewModelInject constructor() : ViewModel() { private val sortOrder = MutableLiveData<String>() val myData = sortOrder.map { Timber.d("Sort order changed to $it") "Sort order is $it" } init { sortOrder.value = "year" } } Observing in Activity class TestActivity : AppCompatActivity() { private val viewModel: TestViewModel by

Kotlin native error unresolved reference coroutines

断了今生、忘了曾经 提交于 2021-02-08 08:59:18
问题 I'm trying to build a native in Windows. I'm not sure where to put the dependency for implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'. My current gradle file looks like this: buildscript { ext.kotlin_version = '1.3.72' repositories { mavenCentral() } dependencies { classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion") } } plugins { id 'org.jetbrains.kotlin.multiplatform' version '1.3.72' } dependencies { implementation 'org.jetbrains.kotlinx:kotlinx

Should a library function be suspend or return deferred

家住魔仙堡 提交于 2021-02-07 20:29:00
问题 Let's assume I'm writing a library that returns a string which is a complex and long running task. I can chose between offering this interface StringGenerator { suspend fun generateString(): String } or interface StringGenerator { fun generateString(): Deferred<String> } Are there any (dis-)advantages of either of the options and which are they? Which should I choose? 回答1: Kotlin coroutines are designed along the "sequential by default" guideline. That means that your API should always expose

Android Kotlin coroutine crashing on strict mode

余生颓废 提交于 2021-02-07 06:08:50
问题 I have created a very simplified version of my issue below. The strict mode is set up with the following policies: StrictMode.setThreadPolicy( StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .penaltyDeath() .build() ) The view model has only one function which crashes the application when invoked. The function does nothing (it has an empty body) class MyViewModel : ViewModel() { fun foo() {

Android Kotlin coroutine crashing on strict mode

落花浮王杯 提交于 2021-02-07 06:02:24
问题 I have created a very simplified version of my issue below. The strict mode is set up with the following policies: StrictMode.setThreadPolicy( StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .penaltyDeath() .build() ) The view model has only one function which crashes the application when invoked. The function does nothing (it has an empty body) class MyViewModel : ViewModel() { fun foo() {

Kotlin coroutine flow example for Android button click event?

那年仲夏 提交于 2021-02-07 02:56:52
问题 I used to use Channel to send out click event from Anko View class to Activity class, however more and more Channel functions are marked as deprecated. So I wanted to start using Flow apis. I migrated code below: private val btnProduceChannel = Channel<Unit>() val btnChannel : ReceiveChannel<Unit> = btnProduceChannel // Anko button { onClick { btnProduceChannel.send(Unit) } } to: lateinit var btnFlow: Flow<Unit> private set button { btnFlow = flow { onClick { emit(Unit) } } } I have to mark