나는 다음과 같은 방식으로 유지 보수 작업을 위해 rxjava을 사용하고와 관찰 가능한 타이머 사용 결과를 방출하는 방법 : 나는 정기적 인 예방 정비가 필요합니다 클래스에서flatMap
을, 나는 관찰 결과 다음과 같은 정적 구독을 사용 클래스가 메모리에로드 될 때 처음 실행되고 지정된 간격으로 시작됩니다.
private static Subscription subscription = Observable.timer(0, 5, TimeUnit.SECONDS)
.flatMap(new Func1<Long, Observable<String>>() {
@Override public Observable<String> call(Long aLong) {
// some code
return Observable.just(null);
}
}).subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread())
.subscribe();
이제는 유지 관리 결과를 UI에보고하고자하는 상황이 있습니다.
일반적으로, 나는 다음과 같은 스키마를
Observable.create(new Observable.OnSubscribe<String>() {
@Override public void call(Subscriber<? super String> subscriber) {
// some code
subscriber.onNext(result);
subscriber.onCompleted(); }
}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String>() {
@Override public void call(String result) {
// write to the UI
}
});
을 사용하지만, 여기에 우리가 한 번만 실행되는 피 감시가 있습니다.
일정한 간격으로 실행되는 Observable의 경우 subscriber.onNext()를 사용하여 결과를 전달할 수 있도록 구독자에서 Action을 호출하는 방법을 찾을 수 없습니다. Observable에 적합한 서명이없는 것 같습니다. timer는 timer()에서 오랜 시간이 걸릴 수 있으며 동시에 작업에 가입 할 수 있습니다. 하지만 rxjava를 알면 트릭을 쓸 수 있습니다 .-)
나는이 작업을 Timer Observable과 Observable (기본적으로 두 버전을 압축)을 사용하여 압축 할 수 있지만, 약간 다른 행동을하기 때문에 첫 번째 구조.
이private static Subscription subscription = Observable.timer(0, 5, TimeUnit.SECONDS)
.flatMap(new Func1<Long, Observable<String>>() {
@Override public Observable<String> call(Long aLong) {
// some code // stays here to ensure there is no concurrency while executing
final String result = "result"; // I store the result in a final variable after some code has been finished
return Observable.create(new Observable.OnSubscribe<String>() {
@Override public void call(Subscriber<? super String> subscriber) {
subscriber.onNext(result); // then I use it in a new Observable and emit it
subscriber.onCompleted(); // not sure if this is needed here (haven't tested this yet)
}
});
}
}).subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread())
.subscribe(new Action1<String>() {
@Override public void call(String result) {
// so I can finally consume the result on the UI thread
}
});
는 대신에 "널 (null)"관찰 가능한을 생성하고 방출, 나는이 하나를 만들 나에게 보낼 수 있습니다 : -
나는 다음과 같은 방식으로 하나에 두 버전을 모두 병합 시도 결과를 구독자에게 보냅니다.
꽤 지저분하지만, 제대로 작동합니까? 더 간단한 해결책? 당신의 생각은 무엇입니까?
왜'Observable.timer.flatMap'가 작동하지 않습니까? – zsxwing
flatMap은 방출 된 Observables를 하나의 Observable (내가 원하는 것)으로 평평하게 만들기 때문에 작동하지 않는다고 생각합니다. 그러나 그걸로 저는 가입자와 인터페이스를 제공하는 서명을 찾지 못합니다 [@Override public void call (Subscriber super String> subscriber)] 그래서 subscriber.onNext()를 사용할 수 있습니다. –