Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.x : why a Subscriber object can be repeatedly used? #5085

Closed
jdsjlzx opened this issue Feb 9, 2017 · 8 comments
Closed

1.x : why a Subscriber object can be repeatedly used? #5085

jdsjlzx opened this issue Feb 9, 2017 · 8 comments

Comments

@jdsjlzx
Copy link

jdsjlzx commented Feb 9, 2017

in this iiues:#3920

@akarnokd said:Subscriber instances are not reusable, you may want to use Observer instead.

The following code into confusion:

private static void test1() {
		Observable observable = Observable.create(new Observable.OnSubscribe<String>(){        
			  @Override        
			   public void call(Subscriber<? super String> subscriber) {        
			        subscriber.onNext("Hello RxJava");    
			        subscriber.onCompleted();    
			   }   
			 });;
		Subscriber subscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {
                System.out.println("onCompleted");
            }
            @Override
            public void onError(Throwable e) {
            	System.out.println("onError");
            }
            @Override
            public void onNext(String value) {
            	System.out.println("onNext value : "+ value);
            }
        };
        
		Subscription subscription = observable.subscribe(subscriber);
		
		System.out.println("----------------");
		
		observable.subscribe(subscriber);
		
		System.out.println("*****************");
		
		Observable.create(new Observable.OnSubscribe<String>(){        
			  @Override        
			   public void call(Subscriber<? super String> subscriber) {        
			        subscriber.onNext("Second");    
			        subscriber.onCompleted();    
			   }   
			 }).subscribe(subscriber);
	}
	private static void test2() {
		Observable observable = Observable.just("Hello RxJava");
		Subscriber subscriber = new Subscriber<String>() {
			@Override
			public void onCompleted() {
				System.out.println("onCompleted");
			}
			@Override
			public void onError(Throwable e) {
				System.out.println("onError");
			}
			@Override
			public void onNext(String value) {
				System.out.println("onNext value : "+ value);
			}
		};
		
		Subscription subscription = observable.subscribe(subscriber);
		
		System.out.println("----------------");
		
		observable.subscribe(subscriber);
		
		System.out.println("*****************");
		
		Observable.create(new Observable.OnSubscribe<String>(){        
			@Override        
			public void call(Subscriber<? super String> subscriber) {        
				subscriber.onNext("Second");    
				subscriber.onCompleted();    
			}   
		}).subscribe(subscriber);
	}

Results are as follows:
test1:

onNext value : Hello RxJava
onCompleted
----------------
onNext value : Hello RxJava
onCompleted
*****************
onNext value : Second
onCompleted

test2:

onNext value : Hello RxJava
onCompleted
----------------
*****************
onNext value : Second
onCompleted

Just because the different way to create observable,test1 method use Observable.create,test2 method use Observable.just,test1 method why subscriber can be repeatedly used?

Whether the cause of the Observable.just & Observable.create ?

@jdsjlzx jdsjlzx changed the title 1.x 1.x : why a Subscriber object can be repeatedly used? Feb 9, 2017
@akarnokd
Copy link
Member

akarnokd commented Feb 9, 2017

The source of the anomaly is your Observable.create (which is not recommended for end-users as it requires advanced knowledge to get right). Observable.just checks if the incoming Subscriber is unsubscribed and won't emit anything while your custom Observables disregard Subscriber.isUnsubscribed().

@jdsjlzx
Copy link
Author

jdsjlzx commented Feb 9, 2017

In addition to just, still have other similar just operator? Such as from, there are other?

Thank you!

@akarnokd
Copy link
Member

akarnokd commented Feb 9, 2017

Don't reuse Subscriber; it is of no benefit if I evaluate what would each operator do with a reused Subscriber. If you want to find out, experiment with them and see for yourself.

@jdsjlzx
Copy link
Author

jdsjlzx commented Feb 9, 2017

Observable.create & Observable.just are all creating observables,why they have such a difference? And the distinction may be difficult to find. The official has a similar document can reference?

@akarnokd
Copy link
Member

akarnokd commented Feb 9, 2017

Observable.create is the main way RxJava's own operators are implemented. These operators honor the protocol of the Observable which you have to ensure when using create. It is unfortunate this became widely used outside of RxJava and due to binary compatibility, we can't change this now with 1.x and we could only put a big warning on its javadoc:

This method requires advanced knowledge about building operators and data sources; please consider other standard methods first, such as fromEmitter(Action1, Emitter.BackpressureMode); Returns an Observable that will execute the specified function when a Subscriber subscribes to it.

@jdsjlzx
Copy link
Author

jdsjlzx commented Feb 9, 2017

Because my English is limited, can't fully understand your reply, thank you first!

@akarnokd
Copy link
Member

Because my English is limited, can't fully understand your reply

I can't help you with that.

@akarnokd
Copy link
Member

I'm closing this issue due to inactivity. If you have further input on the issue, don't hesitate to reopen this issue or post a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants