I'm trying to use these two builders for coroutines in my app, but in my ViewModel I can't import them or they do not pop up.
These are my dependencies:
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc02"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
and in my ViewModel
class MainViewModel(): ViewModel() {
init{
viewModelScope ----> does not work , marked in red
val data = liveData {} ----> does not work, marked in red
}
}
I rebuilt, cleaned, and restarted with invalidating cache, but I can't use them
Add the ViewModel ktx lib:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx$lifecycle_version"
Available after AndroidX lifecycle v2.1.0
for liveData:
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$livedata_version"
for viewModelScope:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
The above additions to build.gradle with cache invalidation and rebuild still were not sufficient to resolve the issue for me (Android Studio Bumblebee | 2021.1.1 Patch 2). Only when I wrote this. I could choose viewModelScope from the pulldown, with automatic import insertion.
Related
I am following this tutorial about RemoteMediator and everything is fine until I run into a weird error when using database.withTransaction{} to allow database operations.
Unfortunately, the IDE doesn't seem (or is refusing) to recognize this very genuine and
legit block. I have checked that I defined ROOM Database abstract class correctly and declared these ROOM and Paging 3 libs in build.gradle(app) file.
// Room
implementation "androidx.room:room-runtime:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
// Room-paging artifact
implementation 'androidx.room:room-paging:2.4.3'
// Paging 3.0
implementation 'androidx.paging:paging-compose:1.0.0-alpha16'
So I decided to ignore the error and went ahead to call a dao function inside the withTransaction{} block but I get Suspension functions can be called only within a coroutine body.
This is a bit confusing since RemoteMediator's load() override function is already a suspend function.
Anyone with insights on this issue please help as I don't seem to be getting anywhere around this.
Thanks to #ianhanniballake day-saving comment above, if you run into this situation just change the room-runtime flavor from:
implementation "androidx.room:room-runtime:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
to room-ktx flavor:
implementation "androidx.room:room-ktx:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
As per Developers Docs ktx-libs provide Kotlin Extensions and Coroutines support which is exactly what database.withTransaction{} block is - it is an extension function on RoomDatabase as shown on this line from the source code.
public suspend fun <R> RoomDatabase.withTransaction(block: suspend () -> R): R {
Android KTX is a set of Kotlin extensions that are included with
Android Jetpack and other Android libraries. KTX extensions provide
concise, idiomatic Kotlin to Jetpack, Android platform, and other
APIs. To do so, these extensions leverage several Kotlin language
features, including the following:
Extension functions
Extension properties
Lambdas
Named parameters
Parameter default values
Coroutines
I just updated our Android project to
implementation 'androidx.appcompat:appcompat:1.2.0'
from
implementation 'androidx.appcompat:appcompat:1.1.0'
And suddenly, the app is very slow.
Information about the project:
MVVM architecture
Using dev.b3nedikt.restring:restring:4.0.3
In the Android documentation it says that:
Deprecated AppCompatDelegate.attachBaseContext(). If you are calling or overriding this method, use AppCompatDelegate.attachBaseContext2() instead.
In all activities, I call:
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(Restring.wrapContext(newBase ?: this)))
}
However, removing it does not seems to solve the problem.
Does anyone have similar issues?
I'm trying to use the new viewModelScope() function provided by the new android ktx library.
In the gradle file, I've added:
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
implementation 'androidx.core:core-ktx:1.0.2'
but when I tied to access the viewModelScope(), I got Unresolved reference: viewModelScope error:
class MainViewModel(application: Application): AndroidViewModel(application) {
fun fetchData(){
viewModelScope.launch{
}
}
}
I don't understand what the problem is. Any idea? Thanks.
Check the release notes https://developer.android.com/jetpack/androidx/releases/lifecycle#declaring_dependencies
viewModelScope is available from v2.1.x
So you need to bump your version in your gradle file.
I have use in my project 2.1.0-beta01:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01"
This is less version then released on official site.
However, I use it because new version 2.2.0-alpha01 still has the Unresolved reference: viewModelScope - Android KTX error.
I'm trying to use AndroidAnnotations #SharefPref within kotlin, but Iget following error
org.androidannotations.annotations.sharedpreferences.Pref can only be used on an element that extends org.androidannotations.api.sharedpreferences.SharedPreferencesHelper
What am I doing wrong?
//Interface
#SharedPref(SharedPref.Scope.APPLICATION_DEFAULT)
open interface MyPreferences {
#DefaultInt(-1)
fun someIntValue():Int
}
//Fragment
#Pref
lateinit open var sharedPref:CongressPreferences_
//usage within fragment
val get: Int = sharedPref.selectedEventId().get()
This is due to a bug in the Kotlin annotation processor.
To fix this, you must add correctErrorTypes = true to your kapt block.
kapt {
correctErrorTypes = true
}
Also make sure you are using the latest Kotlin version (as of this moment: 1.1.3).
I just wanna extend on #WonderCsabo 's answer.
His answer almost saved me, but not fully.
After adding this to my app label build gradle.
kapt {
correctErrorTypes = true
}
I wasn't able to run my app.
Then I closed my android studio and then run Android studio again as administrator.
Voila! it works like charm.
Thank you #WonderCsabo
I'm trying to observe observable on main thread by using:
// Kotlin Code
Observable
.observeOn(AndroidSchedulers.mainThread())
but I'm getting following error:
Type Mismatch:
Required: rx.Scheduler!
Found: io.reactivex.Scheduler!
The Observable I'm subscribing to is from a Library that is written in Java and therefore uses RxJava.
Am i being stupid and missing something? I'm puzzeled :$
Thanks in advance :)
Required: rx.Scheduler!
Required means the signature is Observable.observeOn(rx.Scheduler)
Found: io.reactivex.Scheduler!
Found means the signature is io.reactivex.Scheduler AndroidSchedulers.mainThread()
This means that the Observable is a RxJava 1 observable, while the RxAndroid version used is built for RxJava 2. Since you mentioned that the observable is provided by a library it means that library is built using RxJava 1.
You have 3 options to fix this:
Figure out if the library in question has an RxJava 2 version, or contribute those updates to the project yourself.
Use akarnokd/RxJava2Interop to convert the old Observable to RxJava 2. (RxJavaInterop.toV2Observable(Observable);)
Switch the other dependencies back to RxJava 1.
1.add :
//RX JAVA
implementation "io.reactivex.rxjava3:rxjava:3.0.0-RC6"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
2.Write this code :
val taskObservable: Observable<Task> = Observable.fromIterable(DataSource.createTaskList())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
3.add these imports :
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function
import io.reactivex.schedulers.Schedulers
NOTE :
if you are using KOTLIN, So it is better to use RX Kotlin!
just add two lines in your build.gradle :
//rxkotlin
implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
please include this in your gradle import
rxandroid_version="2.0.1"
implementation "io.reactivex.rxjava2:rxandroid:$rxandroid_version"
add this to your project
import io.reactivex.android.schedulers.AndroidSchedulers
use
.subscribeOn(io.reactivex.rxjava3.schedulers.Schedulers.io())
Try to use these dependencies it works for me :
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.x.x'