我有一個問題,使用rxJava。我需要兩次執行UseCase,但使用不同的參數並在每個變量中添加響應。問題是兩個變量在執行第二個UseCase之前有價值,但我不知道爲什麼。執行多個訂戶rxjava
回答在問題的端
例 執行第一GetCompanies
- >departureTrip
(具有值響應),但returnTrip
具有值,而是在執行getCompaniesReturn
變量聲明
private GetCompanies getCompanies;
private GetCompanies getCompaniesReturn;
private SearchResponseDomain departureTrip;
private SearchResponseDomain returnTrip;
執行
getCompanies.execute(new CompaniesObserver());
getCompaniesReturn.execute(new CompaniesTravelDoubleObserver());
GetCompanies
public class GetCompanies extends UseCase {
private final AppRepository repository;
private String origin;
private String destination;
private String date;
private String passengers;
@Inject
public GetCompanies(AppRepository repository) {
this.repository = repository;
}
@Override
protected Observable buildObservableUseCase() {
return this.repository.getAllResultsSearch(origin, destination, date, passengers);
}
}
用例
public abstract class UseCase {
private Subscription subscription = Subscriptions.empty();
protected UseCase() {
}
@SuppressWarnings("unchecked")
public void execute(Subscriber UseCaseSubscriber) {
this.subscription = this.buildObservableUseCase()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(UseCaseSubscriber);
}
/**
* Unsubscribes from current {@link rx.Subscription}.
*/
public void unsubscribe() {
if (!subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
}
protected abstract Observable buildObservableUseCase();
}
代碼執行getCompanies
個
private final class CompaniesObserver extends
DefaultSubscriber<SearchResponseDomain> {
@Override
public void onCompleted() {
combineResults(departureTrip,returnTrip);
}
@Override
public void onError(Throwable e) {
super.onError(e);
}
@Override
public void onNext(SearchResponseDomain searchResponseDomain) {
super.onNext(searchResponseDomain);
departureTrip = searchMapper.reverseMap(searchResponseDomain);
}
}
代碼執行CompaniesTravelDoubleObserver
private final class CompaniesTravelDoubleObserver extends
DefaultSubscriber<SearchResponseDomain> {
@Override
public void onCompleted() {
super.onCompleted();
combineResults(departureTrip,returnTrip);
}
@Override
public void onError(Throwable e) {
super.onError(e);
}
@Override
public void onNext(SearchResponseDomain searchResponseDomain) {
super.onNext(searchResponseDomain);
returnTrip = searchMapper.reverseMap(searchResponseDomain);
}
}
方法驗證數據完成
public void combineResults(SearchResponsePresentation departureTrip,SearchResponsePresentation returnTrip){
if(departureTrip != null && returnTrip != null){
getView().hideLoading();
getView().showCompanies(departureTrip,returnTrip);
}
}
解
讀取後,並宣讀後和discutions,我發現幫助解決用戶@Hans沃斯特
1 - 我有一個問題,我沒有訪問的觀察到的,我需要在主持人訂閱
GetCompanies
public class GetCompanies //extends UseCase {
{
private final AppRepository repository;
private String origin;
private String destination;
private String date;
private String passengers;
@Inject
public GetCompanies(AppRepository repository) {
this.repository = repository;
}
//@Override
public Observable buildObservableUseCase() {
return this.repository.getAllResultsSearch(origin, destination, date, passengers);
}
public void setOrigin(String origin) {
this.origin = origin;
}
public void setDestination(String destination) {
this.destination = destination;
}
public void setDate(String date) {
this.date = date;
}
public void setPassengers(String passengers) {
this.passengers = passengers;
}
}
演示
Observable<SearchResponseDomain> departureTrip = getCompanies.buildObservableUseCase().subscribeOn(Schedulers.io());
Observable<SearchResponseDomain> returnTrip = getCompaniesReturn.buildObservableUseCase().subscribeOn(Schedulers.io());
// Subs. to both observables. If one finishes -> whole stream closes
Observable<List<SearchResponseDomain>> zip = Observable.zip(departureTrip, returnTrip, (d, r) -> {
return Arrays.asList(d, r);
});
// Subscribe
zip.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CompaniesTravelDoubleObserver());
//------------------------------------------------------------//
private final class CompaniesTravelDoubleObserver extends
DefaultSubscriber<List<SearchResponseDomain>> {
@Override
public void onCompleted() {
getView().hideLoading();
}
@Override
public void onError(Throwable e) {
getView().hideLoading();
networkError.setError(e);
getView().hideLoading();
getView().showError(networkError.getAppErrorMessage());
}
@Override
public void onNext(List<SearchResponseDomain> searchResponseDomains) {
getView().showCompanies(searchMapper.reverseMap(searchResponseDomains));
}
}
這個問題有點不清楚,你能解釋2個變量是什麼,你是說他們有相同的價值? –
這不是你如何做到的。你會使用繼承的功能組合。你能否詳細說明你正在嘗試做什麼,也許還有另一種解決方法來解決你的問題。 –
@HansWurst我添加更多詳細信息 –