使用 Dagger2 在活动之间保持 RxJava 调用

Persist RxJava calls between activities using Dagger2

我在 ExecutionStream class 中有一个 BehaviorRelay 对象,它处理网络调用。请参考ExecutionStream class.

我可以从任何 activity 调用 requestTrackingAndExecution() 方法。我已经实现了 Dagger2 依赖项,这样我就可以在任何 activity.

中注入 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 发出的任何数据<--- 参见此处

我在 @Inject ExecutionStream executionStream;

等两个活动中都注入了 ExecutionStream class

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

中继订阅码:

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

我的ExecutionStreamclass:

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

在 Activity A 运行 requestTrackingAndExecution() 方法之前,Activity B 是否订阅了 internshipAndTrackingRelay

终于找到解决办法了。

我一次又一次地重新初始化我的 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;
    }