BehaviorRelay
中的对象
ExecutionStream
处理网络调用的类。请参阅
班
我可以打电话
requestTrackingAndExecution()
执行流
实例。
我的dagger2配置:
@PerApplication
@Provides
public ExecutionStream provideExecutionStream(PmsApi pmsApi) {
return new ExecutionStream(pmsApi);
}
@PerApplication
@Scope
@Retention(RUNTIME)
public @interface PerApplication { }
我需要做的是:
方法,并在活动B中订阅其发出的数据。
@Inject ExecutionStream executionStream;
为了发射可见光,我打电话给你
internshipAndTrackingRelay.accept(data);
在里面
requestTrackingAndExecution()
订阅中继的代码:
executionStream.internshipAndTracking()
.subscribe(
new Consumer<ExecutionStream.InternshipAndTrackingContainer>() {
@Override
public void accept(ResponseData data){
//do some stuff with responsedata
}
});
我的
执行流
public class ExecutionStream {
@NonNull private PmsApi pmsApi;
@NonNull private final BehaviorRelay<InternExecutionContainer> internExecutionRelay = BehaviorRelay.create();
@NonNull private final BehaviorRelay<InternshipAndTrackingContainer> internshipAndTrackingRelay = BehaviorRelay.create();
public ExecutionStream(@NonNull PmsApi pmsApi) {
this.pmsApi = pmsApi;
}
@NonNull
public Observable<InternshipAndTrackingContainer> internshipAndTracking() {
return internshipAndTrackingRelay.hide();
}
public void requestTrackingAndExecution(String internshipExecutionId, String internExecutionId) {
// Do some network call
// Get response
internshipAndTrackingRelay.accept(new InternshipAndTrackingContainer(responseData));
}
});
}
/**
* This function returns combined response of both apis
* This returns when both apis are finished calling
* @return Observable response
*/
private BiFunction<
InternshipExecutionResponse,
TrackingDataResponse,
TrackingAndExecution>
getMergingBiFuntionForTrackingAndExecution() {
return new BiFunction<InternshipExecutionResponse, TrackingDataResponse, TrackingAndExecution>() {
@Override
public TrackingAndExecution apply(@io.reactivex.annotations.NonNull InternshipExecutionResponse internshipExecutionResponse, @io.reactivex.annotations.NonNull TrackingDataResponse trackingDataResponse) throws Exception {
return new TrackingAndExecution(internshipExecutionResponse,trackingDataResponse);
}
};
}
public class InternshipAndTrackingContainer {
public boolean isError;
public boolean isEmpty;
public TrackingAndExecution trackingAndExecution;
public InternshipAndTrackingContainer() {
this.isError = true;
this.trackingAndExecution = null;
this.isEmpty = false;
}
public InternshipAndTrackingContainer(TrackingAndExecution trackingAndExecution) {
this.trackingAndExecution = trackingAndExecution;
this.isError = false;
this.isEmpty = false;
}
public InternshipAndTrackingContainer(boolean isEmpty) {
this.trackingAndExecution = null;
this.isError = false;
this.isEmpty = isEmpty;
}
}
}