java - Persist RxJava calls between activities using Dagger2 -
i have behaviorrelay
object in executionstream
class handles network calls. please refer executionstream
class.
i can call requesttrackingandexecution()
method activity. have implemented dagger2 dependency such can inject executionstream
instance in activity.
my dagger2 configuration:
@perapplication @provides public executionstream provideexecutionstream(pmsapi pmsapi) { return new executionstream(pmsapi); }
@perapplication
annotation
@scope @retention(runtime) public @interface perapplication { }
what need do: want call requesttrackingandexecution()
method activity , subscribe emitted data in activity b.
currently, suubscriber in activity b not getting data emitted activity a<--- see here
i have injected executionstream class in both activities @inject executionstream executionstream;
for emitting observable, calling internshipandtrackingrelay.accept(data);
in requesttrackingandexecution()
method after getting data network call.
code subscribing relay:
executionstream.internshipandtracking() .subscribe( new consumer<executionstream.internshipandtrackingcontainer>() { @override public void accept(responsedata data){ //do stuff responsedata } });
my executionstream
class:
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) { // network call // response internshipandtrackingrelay.accept(new internshipandtrackingcontainer(responsedata)); } }); } /** * function returns combined response of both apis * returns when both apis 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; } } }
finally found solution.
i reinitializing applicationmodule again , again.
changed this:
public applicationcomponent getcomponent() { applicationcomponent component = daggerapplicationcomponent.builder() .networkmodule(new networkmodule()) .applicationmodule(new applicationmodule(this)) .build(); return component; }
to this:
public synchronized applicationcomponent getcomponent() { if(component == null) { component = daggerapplicationcomponent.builder() .networkmodule(new networkmodule()) .applicationmodule(new applicationmodule(this)) .build(); } return component; }
Comments
Post a Comment