gpt4 book ai didi

java - 使用 Dagger2 在 Activity 之间保留 RxJava 调用

转载 作者:行者123 更新时间:2023-12-02 12:30:11 26 4
gpt4 key购买 nike

我在 ExecutionStream 类中有一个 BehaviorRelay 对象,用于处理网络调用。请引用ExecutionStream类。

我可以从任何 Activity 调用 requestTrackingAndExecution() 方法。我已经实现了 Dagger2 依赖项,这样我就可以在任何 Activity 中注入(inject) ExecutionStream 实例。

我的 dagger2 配置:

@PerApplication
@Provides
public ExecutionStream provideExecutionStream(PmsApi pmsApi) {
return new ExecutionStream(pmsApi);
}

@PerApplication注释

@Scope
@Retention(RUNTIME)
public @interface PerApplication { }

我需要做什么:我想从 Activity A 调用 requestTrackingAndExecution() 方法并在 Activity B 中订阅其发出的数据。

目前, Activity B 中的订阅者未获取 Activity A 发出的任何数据<--- 请参阅此处

我已经在两个 Activity 中注入(inject)了 ExecutionStream 类,例如 @Inject ExecutionStreamexecutionStream;

为了发出可观察的数据,我在从网络调用获取数据后在 requestTrackingAndExecution() 方法中调用 internshipAndTrackingRelay.accept(data);

订阅中继代码:

executionStream.internshipAndTracking()
.subscribe(
new Consumer<ExecutionStream.InternshipAndTrackingContainer>() {
@Override
public void accept(ResponseData data){
//do some stuff with responsedata
}
});

我的ExecutionStream类:

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;
}
}
}

最佳答案

终于找到解决办法了。

我一次又一次地重新初始化我的 ApplicationModule。

更改了此内容:

public ApplicationComponent getComponent() {
ApplicationComponent component = DaggerApplicationComponent.builder()
.networkModule(new NetworkModule())
.ApplicationModule(new ApplicationModule(this))
.build();

return component;
}

对此:

public synchronized ApplicationComponent getComponent() {
if(component == null) {
component = DaggerApplicationComponent.builder()
.networkModule(new NetworkModule())
.ApplicationModule(new ApplicationModule(this))
.build();
}
return component;
}

关于java - 使用 Dagger2 在 Activity 之间保留 RxJava 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45305032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com