使用Dagger2

时间:2017-07-25 13:37:43

标签: java android rx-java2

我在BehaviorRelay类中有一个ExecutionStream对象来处理网络调用。请参阅ExecutionStream课程。

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

我的dagger2配置:

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

@PerApplication注释

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

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

目前,活动B中的suubscriber没有从活动A中发出任何数据< ---见这里

我在@Inject ExecutionStream executionStream;

这两个活动中注入了ExecutionStream类

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

订阅中继的代码:

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

2 个答案:

答案 0 :(得分:1)

终于找到了解决方案。

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

答案 1 :(得分:0)

在活动A运行requestTrackingAndExecution()方法之前,活动B是否订阅了Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null); if (cursor.moveToFirst()) { // must check the result to prevent exception do { String msgData = ""; for(int idx=0;idx<cursor.getColumnCount();idx++) { msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx); } // use msgData } while (cursor.moveToNext()); }