android - Converting this AsyncTask to RxAndroid? -
bit new rx, looking on converting following asynctask
rx, can visualize rx bit more code know something. i've found few other answers relevant, alot of them werent network requests , many used different operators different answers, bit confused.
heres asynctask
:
public class backgroundstuff extends asynctask<void, void, void> { @override protected void doinbackground(void... params) { useragent myuseragent = useragent.of("xxx:xxxx:xxx"); redditclient redditclient = new redditclient(myuseragent); credentials credentials = credentials.userlessapp("xxxxxxxx", uuid.randomuuid()); oauthdata authdata = null; try { authdata = redditclient.getoauthhelper().easyauth(credentials); redditclient.authenticate(authdata); } catch (oauthexception e) { e.printstacktrace(); } subredditpaginator sp = new subredditpaginator(redditclient); sp.setlimit(7); sp.setsorting(sorting.hot); sp.settimeperiod(timeperiod.day); sp.setsubreddit("pics"); sp.next(true); listing<submission> list = sp.getcurrentlisting(); submission sub = list.get(1); (submission submission : list) { log.d("test", "title: " + submission.gettitle()); } submission sub2 = redditclient.getsubmission(sub.getid()); system.out.println(sub2.getcomments().get(0).getcomment().getbody()); return null; } }
for reference, authenticates reddit , grabs results particular subreddit.
i know need observable
, , .suscribeon(schedulers.newthread())
, .observeon(androidschedulers.mainthread())
, not sure how make observable.
wrap in function:
void dosomething(){ useragent myuseragent = useragent.of("xxx:xxxx:xxx"); redditclient redditclient = new redditclient(myuseragent); credentials credentials = credentials.userlessapp("xxxxxxxx", uuid.randomuuid()); oauthdata authdata = null; try { authdata = redditclient.getoauthhelper().easyauth(credentials); redditclient.authenticate(authdata); } catch (oauthexception e) { e.printstacktrace(); } subredditpaginator sp = new subredditpaginator(redditclient); sp.setlimit(7); sp.setsorting(sorting.hot); sp.settimeperiod(timeperiod.day); sp.setsubreddit("pics"); sp.next(true); listing<submission> list = sp.getcurrentlisting(); submission sub = list.get(1); (submission submission : list) { log.d("test", "title: " + submission.gettitle()); } submission sub2 = redditclient.getsubmission(sub.getid()); system.out.println(sub2.getcomments().get(0).getcomment().getbody()); return null; }
wrap in observable:
observable.just(dosomething);
wrap in defer run when subscribes it:
observable<void> dosomethingobservable = observable.defer(()-> observable.just(dosomething))
decide run subscribeon , observeon, , subscribe make run:
dosomethingobservable .suscribeon(schedulers.newthread()) .observeon(androidschedulers.mainthread()).subscribe();
Comments
Post a Comment