Reputation: 11416
I want to use RxAndroid and RxJava in my project. So, in the build.gradle file a added the lines shown below. but, when I tried to use
animalsObserver.subscribeOn();//unrecognized
it is not recognized by AndroidStudio. So, please let me know how to correctly use .subscribeOn() method.
code:
import rx.Observable;
import rx.Observer;
public class MainActivity extends AppCompatActivity {
Observable<String> animalsObservable = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animalsObservable = Observable.just("Ant", "Bee", "Cat", "Dog", "Fox");
Observer<String> animalsObserver = getAnimalsObserver();
animalsObserver.subscribeOn();//unrecognized
}
private Observer<String> getAnimalsObserver() {
return new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe");
}
@Override
public void onNext(String s) {
Log.d(TAG, "Name: " + s);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.d(TAG, "All items are emitted!");
}
};
}
}
gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.9'
testCompile 'junit:junit:4.12'
}
Upvotes: 1
Views: 1701
Reputation: 557
Call the method subsribeOn() on the Observable not on Observer. So modify your code as below:
animalsObservable
.subscribeOn(Scheduler.io()) //run on background thread
.observerOn(AndroidScheduler.mainThread())
.subscribe(animalsObserver);
Upvotes: 0
Reputation: 9190
It's Observable
that has subcribeOn()
api, not Observer
. Your example should be something like:
animalsObservable.subscribeOn(AndroidSchedulers.mainThread()).subscribe(getAnimalsObserver())
Upvotes: 1
Reputation: 1184
In subscribeOn() method you have to pass Scheduler on which you observer will be executed You need subscribe() method;
Upvotes: 0