reactive programming - RxJava cache intermediate results -
i'm developing android client kaltura video platform, , develop uploading video. feature consists of following steps:
create mediaentry
params: name, description
create uploadtoken
params: filename, mediaentryid(which received @ step 1)
addcontent - in other words bind mediaentry uploadtoken
params: mediaentryid(which received @ step 1), uploadtokenid(which received @ step 2)
uploadvideo params: uploadtokenid(which received @ step 2), videodata
here code using rxjava:
api.createmediaentry(name, description) .flatmap(mediaentry -> { mediaentryid = mediaentry.getid(); return api.createuploadtoken(this.videouri.getpath(), mediaentry.getid()); }) .flatmap(uploadtoken -> { uploadtokenid = uploadtoken.getid(); return api.addcontent(mediaentryid, uploadtoken.getid()); }) .flatmap(mediaentry -> api.uploadvideo(uploadtokenid, fileutil.gettypedvideofilefromuri(this.videouri, uploadvideoactivity.this))) .doonerror(throwable -> { throwable.printstacktrace(); dismissprogressdialog(); }) .doonnext(response -> { }) .dooncompleted(() -> { dismissprogressdialog(); }) .subscribe();
where api retrofit rest adapter.
as can see code, need save mediaentryid @ step 1, , uploadtokenid @ step 2, use them @ step 3. want undestand, there way save mediaentry step 1 , uploadtoken step 2 using them later, or may there way change code, exclude saving data local variables?
ideal solution me looks this:
api.createmediaentry(name, description) .dosomethingwithmedaientry(mediaentry -> mediaentry) .flatmap(mediaentry -> api.createuploadtoken(this.videouri.getpath(), mediaentry.getid())) .dosomethingwithuploadtoken(token -> token) .getmediaentryanduploadtokensomehow((mediaentry, uploadtoken) -> api.addcontent(mediaentry.getid(), uploadtoken.getid())) .getuploadtokensomehow(uploadtoken -> api.uploadvideo(uploadtoken.getid(), fileutil.gettypedvideofilefromuri(this.videouri, uploadvideoactivity.this))) .doonerror(throwable -> { throwable.printstacktrace(); dismissprogressdialog(); }) .doonnext(response -> { }) .dooncompleted(() -> { dismissprogressdialog(); }) .subscribe();
aside using tuple, can nest observables.
api.createmediaentry(name, description) .flatmap(createdmediaentry -> api.createuploadtoken(this.videouri.getpath(), createdmediaentry.getid()) .flatmap(uploadtoken -> api.addcontent(createdmediaentry.getid(), uploadtoken.getid()) .flatmap(addedmediaentry -> api.uploadvideo(uploadtoken.getid(), , , )) ) ) ...