java - Android application becomes slow after multiple callbacks - what's wrong here? -
i have application parses through user's facebook messages, , due extremely annoying limitation facebook api lets receive 25 messages per api call, have use weird quasi-recursive method parse through messages can handle pagination. issue arose when attempted make application sleep in scenario exceeded requests before trying again.
i noticed app shoot out fast, parsing through several api calls per second. app dragged on , requests hit several hundreds, slowed crawl of maybe 1 per 5 seconds. see slowing me down here?
relevant portions of code:
i start parsing service, makes initial request 25 messages (and links every 1 after that) , calls retrievemsgs().
@override public int onstartcommand(intent intent, int flags, int startid){ if(intent!=null) { id = intent.getstringextra("id"); } session session = session.getactivesession(); new request(session, id + "/comments", null, httpmethod.get, new request.callback() { @override public void oncompleted(response response) { try { jsonarray msgs = response.getgraphobject().getinnerjsonobject().getjsonarray("data"); populatedata(msgs); } catch (jsonexception e) { e.printstacktrace(); } request next = response.getrequestforpagedresults(response.pagingdirection.next); retrievemsgs(next); } }).executeasync(); return start_sticky; }
and calls retrievemsgs, call until finishes parsing each 25 message array facebook api. parses 25 messages , checks see if received link next page, , quits if doesn't.
public void retrievemsgs(final request next) { log.i("api calls", apicalls+""); if (next != null) { next.setcallback(new request.callback() { public void oncompleted(response response) { try { if (response.getgraphobject() == null) { // exceeded our limit, sleep bit before trying again try { thread.sleep(240000); } catch(exception e){ } retrievemsgs(next); // restart method call log.i("hello", "is slowing down?"); return; } jsonarray msgs = response.getgraphobject().getinnerjsonobject().getjsonarray("data"); populatedata(msgs); request next2 = response.getrequestforpagedresults(response.pagingdirection.next); if (next2 != null) { apicalls++; retrievemsgs(next2); } else { showdonenotif(); }} catch (jsonexception e) { e.printstacktrace(); } } }); next.executeasync(); } }
i have difficulty tracking code through inner classes , callbacks , whatnot, i'd appreciate if explain potentially going wrong here. slowdown kills viability of app after 20,000 messages or so, , potentially hundreds of thousands of messages per conversation, can't reasonably move on this.
using thread.sleep()
in android app no-no.
create handler
in activity
:
handler handler = new handler();
then use schedule method run later:
handler.postdelayed(new runnable() { public void run() { retrievemsgs(next); } }, 240000);
the handler
schedules task on current thread
.
see docs: handler | android developers
Comments
Post a Comment