Category : kotlin-coroutines

I am making a recyclerview to display blog posts using Paging 3. The repository returns a Flow object. I want to get the tags associated with each blog post (which is a class member of BlogPost) “`private val _tags = MutableLiveData<Set<String>>() val tags: LiveData<Set<String>> get() = _tags private val _blogPost = repository.getBlogPosts() .map { posts ..

Read more

I am using Ktor for websockets for an Android project and have it working pretty well. I have noticed though that if I disconnect from WiFi and reconnect, and then try send data to an open channel, then I get the following exception: TxCallSocket$callListen: java.util.concurrent.CancellationException: ArrayChannel was cancelled at kotlinx.coroutines.channels.AbstractChannel.cancel(AbstractChannel.kt:669) at kotlinx.coroutines.channels.ReceiveChannel$DefaultImpls.cancel$default(Channel.kt:308) at io.ktor.http.cio.websocket.DefaultWebSocketSessionImpl$runOutgoingProcessor$1.invokeSuspend(DefaultWebSocketSessionImpl.kt:182) at ..

Read more

I have a broadcast receiver for wifi scan results as a data source and I’d like to make it in coroutine way. I found an answer for suspend function here: https://stackoverflow.com/a/53520496/5938671 suspend fun getCurrentScanResult(): List<ScanResult> = suspendCancellableCoroutine { cont -> //define broadcast reciever val wifiScanReceiver = object : BroadcastReceiver() { override fun onReceive(c: Context, intent: ..

Read more

I am getting a NullPointerException and invokeSuspend error when trying to get data from a Room DAO using a Kotlin coroutine. Do I need to do something special in order to use room and coroutines work together? The Error E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-4 Process: com.school, PID: 29799 java.lang.NullPointerException: Attempt to invoke virtual method ‘boolean java.lang.Boolean.booleanValue()’ ..

Read more

I want call hight order suspend function from other class with parameter and i don’t know how. class CharactersListViewModel : ViewModel() { private val dataSourceFactory = PageKeyDataSourceFactory( scope = viewModelScope, request = suspend {createRequest(0) } ) private suspend inline fun createRequest(offset : Int): MutableList<CharacterItem> { val repository = Injection.provideMarvelRepository() val response = repository.getCharacters( offset = ..

Read more

I want to create a shared view model for communication between MainActivity to fragments. I decided to use share flow for managing events. private val _sharedChannel: MutableSharedFlow<SharedEvent> = MutableSharedFlow( replay = 0,extraBufferCapacity=0,onBufferOverflow = BufferOverflow.SUSPEND) val sharedChannel = _sharedChannel.asSharedFlow() I don’t need to cache the last event, not even when orientation changes.. so I set "replay ..

Read more