Category : kotlin-coroutines

I have the following in my viewModel fun getUsernamePassword(inputUserName:String, inputPassword:String):List<User> = viewModelScope.launch(Dispatchers.IO) { return repository.getUsernamePassword(inputUserName, inputPassword) } However, I am getting an error saying Type mismatch. Required: List<User> Found:Job What can I do to solve this error. The ultimate goal is to have the function run on a non-black thread operate and a part from ..

Read more

Until now I was using Flow and mapping it to LiveData like below – The MyService looks like this – override fun provideData(action: MyAction) = flow { emit(MyResult.Loading) emit(dataRepository.getNewData()) } The ViewModel looks like this – fun getData() = myService.provideData(MyAction.GetData).map { }.asLiveData(Dispatchers.Default + viewModelScope.coroutineContext) I want to move to StateFlow. How can I use emit ..

Read more

I’m collecting a flow on the viewLifecycleOwner. It flows on Dispatchers.Default, but the collection itself takes place on Dispatchers.Main. override fun onViewCreated(view: View, savedInstanceState: Bundle?) { viewLifecycleOwner.lifecycleScope.launch { flow.flowOn(Default).collect { requireContext() } } } In one occation I found the IllegalStateException stating, that the fragment is not attached. IllegalStateException: Fragment Test not attached to a ..

Read more

I’m trying to load and display books from the books api but the information doesn’t display. I was able to get it to display if I used the debugger and step through but not from starting off. Adapter.kt private var dataList: MutableList<BookItem> = mutableListOf() fun addItems(bookItems : List<BookItem>) { dataList.addAll(bookItems) notifyDataSetChanged() } Fragment.kt override fun ..

Read more

Trying to upgrade to version 3 of paging and i am starting to have difficulties since importing the library … seems like it is having conflicts to work side to side of coroutines Duplicate class kotlinx.coroutines.AbstractCoroutine found in modules kotlinx-coroutines-core-1.3.9.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9) and kotlinx-coroutines-core-jvm-1.3.9.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.3.9) Duplicate class kotlinx.coroutines.Active found in modules kotlinx-coroutines-core-1.3.9.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9) and kotlinx-coroutines-core-jvm-1.3.9.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.3.9) ..

Read more

I am using HILT for DI. I am able to network call in Activity/Fragment but unable to understand how to network call in Service. Below is my Application Module @Module @InstallIn(ApplicationModule::class) class ApplicationModule { @Provides fun provideBaseUrl() = AppConfig.get().getBaseAPIUrl() @Provides @Singleton fun provideOkHttpClient() = if (BuildConfig.DEBUG) { val loggingInterceptor = HttpLoggingInterceptor() loggingInterceptor.apply { loggingInterceptor.level = ..

Read more

I’m learning android liveData. have the code: private lateinit var questionCounterTotal: LiveData<Int> inside fragment’s onCreateView: questionList = questionViewModel.getAllQuestions() questionCounterTotal = liveData { emit(44) } //this line prints null questionCounterTotal.observe(viewLifecycleOwner,Observer<Int>{value-> Log.d("amin",value.toString()) }) //this line prints null too Log.d("amin",questionCounterTotal.value.toString()) As comments describe,i cant print questionCouterTotal value.what am i doing wrong? Source: Android..

Read more

The first item(oldest item) added in the database can be deleted but when I try to delete a item from between or the latest item added in the database then the following error is occurring. As illustrated below: ITEM 1(latest item added in the database) -> Error ITEM 2 -> Error ITEM 3 -> Error ..

Read more

I created an abstract Kotlin Coroutine Task class but I receive an error when I run app.. Android Studio when I write: extends Corou… it suggests me CoroutineTask<Params, Progress, Result> so it recognized, no syntax errors. Someone could help me? Thanks error: cannot find symbol public class AsyncCall extends CoroutineTask<String,String,String>{ ^ symbol: class CoroutineTask Coroutine ..

Read more