-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample3.java
More file actions
60 lines (49 loc) · 1.65 KB
/
example3.java
File metadata and controls
60 lines (49 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
/**
* Uses create() method, and uses a different logic
* on scenarios like onNext, onError, onCompleted.
*
* Creates an {@link Observable} which pushes values to the subscriber.
*/
public class example3 {
public static void main(String[] args){
//create your source of data that will emit to subscriber/observers
Observable myObservable = Observable.create(new Observable.OnSubscribe(){
@Override
public void call(Object subscriber) {
Subscriber mySubscriber = (Subscriber)subscriber;
for(int i = 0 ; i < 10; i++){
//if my subscriber was subscribed
if(!mySubscriber.isUnsubscribed()){
mySubscriber.onNext("Pushed value " + i);
}
}
if(!mySubscriber.isUnsubscribed()){
mySubscriber.onCompleted();
}
}
});
//subscribe to your Observable
myObservable.subscribe(new Action1<String>() {
@Override
public void call(String s) {
System.out.println(s);
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
System.out.println("Something went wrong the observable");
}
},
new Action0() {
@Override
public void call() {
System.out.println("No more values will be pushed.");
}
});
}
}