kotlin-coroutines

Starting a coroutine inside onBindViewHolder creates a mess with recycler

一世执手 提交于 2020-01-16 09:08:18
问题 I am developing a chat application and there is a specific API so some things i must implement them with a specific way. For example (and the case that i have a problem...) When i have to display an Image the API says that i have to split the Image in small chunks and store them as a message with a byteArray content. There is also a header message that its body is the messageIds of the fileChunks. So in the RecyclerView inside the onBindViewHolder, when i see a header file message (msgType ==

“Cannot access database on the main thread since it may potentially lock the UI for a long period of time.” error on my Coroutine

♀尐吖头ヾ 提交于 2020-01-15 14:25:34
问题 My Coroutine is running on the main thread, which i've specified in my Coroutine context: class ClickPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs), CoroutineScope, View.OnClickListener { override val coroutineContext: CoroutineContext get() = Dispatchers.Main override fun onClick(v: View?) { when (key){ "logout" -> { CoroutineScope(coroutineContext).launch { CustomApplication.database?.clearAllTables() Log.d("MapFragment", "Cleared Tables") } if (Profile

Kotlin CoroutineScope can't cancel in android views

試著忘記壹切 提交于 2020-01-13 18:27:52
问题 For example,this view.When the onDetachedFromWindow invoke the scope is cancelled,but the launched job is still active. class TestView : FrameLayout,CoroutineScope { val TAG = "TestView" override val coroutineContext: CoroutineContext get() = Dispatchers.Main + Job() constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { launch { while (true) { Log.i(TAG,"is in launch coroutine....${coroutineContext} ${this@TestView

Kotlin CoroutineScope can't cancel in android views

萝らか妹 提交于 2020-01-13 18:27:27
问题 For example,this view.When the onDetachedFromWindow invoke the scope is cancelled,but the launched job is still active. class TestView : FrameLayout,CoroutineScope { val TAG = "TestView" override val coroutineContext: CoroutineContext get() = Dispatchers.Main + Job() constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { launch { while (true) { Log.i(TAG,"is in launch coroutine....${coroutineContext} ${this@TestView

How to return a list from Firestore database as a result of a function in Kotlin?

空扰寡人 提交于 2020-01-11 13:48:09
问题 I'm building an app for a friend and I use Firestore. What I want is to display a list of favorite places but for some reason, the list is always empty. I cannot get the data from Firestore. This is my code: fun getListOfPlaces() : List<String> { val places = ArrayList<String>() placesRef.get().addOnCompleteListener { task -> if (task.isSuccessful) { for (document in task.result) { val name = document.data["name"].toString() places.add(name) } } } return list; } If I try to print, let's say

Handle exceptions thrown by a custom okhttp Interceptor in Kotlin Coroutines

元气小坏坏 提交于 2020-01-05 03:32:29
问题 I'm using a custom Interceptor along with Retrofit client in my Android app, that throws an Exception under some specific circumstances. I'm trying to make it work using Kotlin coroutines. The problem is that I'm unable to handle the before mentioned error, since in the moment the exception is thrown from within the Interceptor instance, it crashes the whole app instead of being caught in the coroutine's try/catch statement. While I was using the Rx implementation, the exception was

Suspend coroutine until condition is true

风格不统一 提交于 2020-01-03 02:51:08
问题 I have a use case where I need to connect and disconnect from a class that acts as a service. Actions can be performed on the service only when the service is connected. Clients are notified when the service connects or disconnects by a callback: class Service { constructor(callback: ConnectionCallback) { ... } fun connect() { // Call callback.onConnected() some time after this method returns. } fun disconnect() { // Call callback.onConnectionSuspended() some time after this method returns. }

When to use Kotlin suspend keyword?

二次信任 提交于 2020-01-02 07:29:14
问题 fun startAsyncFunc() { launch { asyncFunc1() asyncFunc2() } } fun asyncFunc1() { ... } suspend fun asyncFunc2() { ... } I can finish the work without suspend and it even makes test easier (it can be tested without adding runBlocking . My questions: asyncFunc1 vs asyncFunc2 , which is better and why? If asyncFunc2 is better, should I always use suspend whenever a function will be ran in the coroutines? Update In the recent releases of Kotlin Coroutines, I notice if a method doesn't contain any

PublishSubject with kotlin coroutines (Flow)

被刻印的时光 ゝ 提交于 2020-01-02 02:37:05
问题 I'm trying to refactor a piece of code from RX to coroutines but despite all my efforts I think I'm getting lost. So I had PublishSubject created and i was sending messages to it and also I was listening for results. It worked flawlessly, but now I'm not sure how to do the same thing with coroutines (flows or channels). private val subject = PublishProcessor.create<Boolean>>() ... fun someMethod(b: Boolean) { subject.onNext(b) } fun observe() { subject.debounce(500, TimeUnit.MILLISECONDS)

How to use Fuel with a Kotlin coroutine

回眸只為那壹抹淺笑 提交于 2019-12-30 11:10:12
问题 Within an Android app, I'm trying to use Fuel to make an HTTP request within a Kotlin coroutine. My first try is to use the synchronous mode inside a wrapper like this: launch(UI) { val token = getToken() println(token) } suspend fun getToken(): String? { var (request, response, result = TOKEN_URL.httpGet().responseString() return result.get() } But that is returning an android.os.NetworkOnMainThreadException . The Fuel documentation mentions .await() and .awaitString() extensions but I haven