It is advisable to use either Func0 or Action1 in rxjava, what is the difference

I have the following code snippet where Func0and are used Action1.

Observable.defer(new Func0<Observable<String>>() {
    @Override
    public Observable<String> call() {
        try {
            return Observable.just(Database.readValue());       
        }
        catch(IOException e) {
            return Observable.error(e);     
        }   
    })
    .subscribe(new Action1<String>() {
        @Override
        public void call(String result) {
            resultTextView.setText(result);     
        }   
    }
}

But I'm just wondering what the difference is between the two. I understand that a number means the number of parameters, i.e. Func0It has no parameters, but Action1has 1 parameter.

However, how do you know which one to use? Should I use Actionor Func.

What is the purpose of the method call?

Thanks so much for any suggestions,

+4
source share
2 answers

Short answer; You will find out which method you are calling.

First, let's look at two methods that you are trying to use:

Observable.defer Observable, Observable factory, Observable , . , , factory.

: observableFactory Observable factory , Observable

: , Observable factory

public final static <T> Observable<T> defer(Func0<Observable<T>> observableFactory)...

Observable.subscribe , .

: onNext Action1, ,

: , , Observable .

public final Subscription subscribe(final Action1<? super T> onNext)...

, , - Strategy Pattern, .

defer Observable . A Func0 , ( R - Observable<String>):

public interface Func0<R> extends Function, Callable<R> {
    @Override
    public R call();
}

subscribe . Action1 ( T1 - String)

public interface Action1<T1> extends Action {
    public void call(T1 t1);
}

new Action1<>() {...} new Func0<>() {...}, . , , Action1.call Func0.call.


:

, ? Action Func.

. , . , .

?

/, , . , . call. foo, bar.

+4

:

interface Func0<R> {
    R call();
}

interface Action1<T> {
    void call(T t);
}

Func0 , Action1 . , .

+5

Source: https://habr.com/ru/post/1695135/


All Articles