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'
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'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.
I intent to use the Fuel library inside an Android project.
I added the following to my Gradle file:
//core
implementation 'com.github.kittinunf.fuel:fuel:2.1.0'
//packages
implementation 'com.github.kittinunf.fuel:fuel-android:2.1.0'
implementation 'com.github.kittinunf.fuel:fuel-json:2.1.0'
It was succesfully resolved.
I copied the quick start code to a class, and when I try to compile, I get the following:
What else is required?
Make sure you are importing:
import com.github.kittinunf.result.Result
Otherwise it tries to use Kotlin's Result class.
I am trying to pass data between fragments using navigation controller. in this documentation: https://developer.android.com/topic/libraries/architecture/navigation/navigation-pass-data
it is said that I can send data using bundle, but when I type the code below:
var bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)
I have a message: Bundle' is deprecated. Use Android KTX version ?
what should I do ? I don't understand about that 'Use the Android KTX version'
I finally find the answer,
so first add dependecy:
dependencies {
implementation 'androidx.core:core-ktx:1.0.0'
}
then import this:
import androidx.core.os.bundleOf
and finally the warning will dissapear
I think it's requesting that top level function: https://developer.android.com/reference/kotlin/androidx/core/os/package-summary#bundleOf%28kotlin.Pair%29
The import line should be something like: import androidx.core.os.bundleOf
I am encountering following error while building an apk:
error: package android.arch.core.util does not exist
My java code has a line:
import android.arch.core.util.Function;
I know this exists and its doc is here.
But how can I import it?
There was a versioning problem and my problem solved with this link:
https://issuetracker.google.com/issues/66894924#comment12
I used beta version of libs and it solved the error about android.arch.core.util.Function;
I'm not sure what you are using it for but here is a sample that compiles and runs. Add the import to the class that needs it and add the code somewhere in that same class. Android Studio would not let me import it until I had code that actually used it. It may be a bug or intentional.
Import:
import android.arch.core.util.Function;
Code:
Function function = new Function<Boolean, String>() {
#Override
public String apply(Boolean input) {
return null;
}
};
Please make sure you have added the corresponding dependency in gradle
Example
implementation "android.arch.lifecycle:extensions:1.1"
Final answer
Add the below code in app/build.gradle file under "dependencies" block
// Room components
implementation "android.arch.persistence.room:runtime:1.1.0”
annotationProcessor "android.arch.persistence.room:compiler:1.1.0"
androidTestImplementation "android.arch.persistence.room:testing:1.1.0"
// Lifecycle components
implementation "android.arch.lifecycle:extensions:1.1.0"
annotationProcessor "android.arch.lifecycle:compiler:1.1.0"