代码之家  ›  专栏  ›  技术社区  ›  Harshil Pansare

使用Dagger2在活动之间持久化RxJava调用

  •  0
  • Harshil Pansare  · 技术社区  · 9 年前

    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;
        }
    }
    }
    
    2 回复  |  直到 9 年前
        1
  •  1
  •   Harshil Pansare    9 年前

    终于找到了解决方案。

    我一次又一次地重新初始化我的应用程序模块。

    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;
        }
    
        2
  •  0
  •   ajplumlee33    9 年前

    活动B是否订阅 internshipAndTrackingRelay 在活动A运行 requestTrackingAndExecution() 方法