Reputation: 632
I created simple activity with infinity progres bar, and I'am trying to run time consuming method using RxJava to prevent UI thread from blocking, but everytime UI thread is blocked. I think my solution has problem with emitting Observable. Can anyone help me? I'am begginer in RX.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doSomething(View view) {
doHeavyStuff()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Action1() {
@Override
public void call(Object o) {
Toast.makeText(getApplicationContext(), "FINISHED", Toast.LENGTH_SHORT).show();
}
})
.subscribe();
}
private Observable doHeavyStuff() {
for (int i = 0; i < 999999999; i++) {
for (int j = 0; j < 2; j++) {
}
}
return Observable.just(1);
}
Upvotes: 23
Views: 16179
Reputation: 36049
With RxJava2 a possible solution is:
Single.fromCallable(() -> loadInBackground())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((resultObject) -> { updateUi(resultObject) });
Single.fromCallable(new Callable<Object>() {
@Override
public Object call() throws Exception {
return loadInBackground();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Object>() {
@Override
public void accept(Object resultObject) throws Exception {
updateUi(resultObject);
}
});
private Object loadInBackground() {
// some heavy load code
return resultObject;
}
private void updateUi(Object resultObject) {
// update your Views here
}
Upvotes: 36
Reputation: 5329
kotlin use below code to work in background
Single.fromCallable {
// method that run in background
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
Upvotes: 12
Reputation: 2247
Your doHeavyStuff() executes computation on calling thread, you just wrap your result into Observable. In order to wrap computation into observable you should use defer
Observable.defer(new Func0<Observable<Integer>>() {
@Override
public Observable<Integer> call() {
return Observable.just(doHeavyStuff());
}
});
then you can specify threads by subscribeOn and observeOn methods
Upvotes: 10
Reputation: 4584
According to docs
Deprecated: fromFunc0 Unnecessary now that Func0 extends Callable. Just call fromCallable(java.util.concurrent.Callable) instead.
So you could make the call in this way:
Observable.fromCallable(new Callable<Object>() {
@Override
public Object call() throws Exception {
return someMethod();
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Object>() {
@Override
public void call(Object object) {
}
});
Upvotes: 11
Reputation: 423
Also, you can use RxJavaAsyncUtil:
compile 'io.reactivex:rxjava-async-util:0.21.0'
Code:
Observable.fromFunc0(() -> doHeavyStuff())
Upvotes: 1