java - Code structure with Asynctask Android -
i guess producing "spaghetti-code". perhabs experts can me write better code solution. following pseudocodes show problem:
i have procedures runs long. procedures show progressbar. use asynctasks achieve this. works fine, code not maintainable anymore :
my wish leave simple code without nesting code :
public void oncreate(bundle savedinstancestate) { longrunningtask1(); dosomething1(); longrunningtask2(); dosomething2(); longrunningtask3(); dosomething3(); longrunningtask4(); dosomething4(); }
my code asynctasks :
public void oncreate(bundle savedinstancestate) { new longrunningtask1().execute("task1"); } class longrunningtask1 extends asynctask<string, void, boolean> { private exception exception; protected rssfeed doinbackground(string... urls) { try { do; return true; } catch (exception e) { return null; } } protected void onprogressupdate(void... values) { updateprogressbar(); } protected void onpostexecute(rssfeed feed) { dosomething1(); new longrunningtask2().execute("task2"); } } class longrunningtask2 extends asynctask<string, void, boolean> { similar longrunningtask1 protected void onpostexecute(rssfeed feed) { dosomething2(); new longrunningtask3().execute("task3"); } } class longrunningtask3 extends asynctask<string, void, boolean> { similar longrunningtask2 protected void onpostexecute(rssfeed feed) { dosomething3(); new longrunningtask4().execute("task4"); } } class longrunningtask4 extends asynctask<string, void, boolean> { similar longrunningtask3 protected void onpostexecute(rssfeed feed) { dosomething4(); } }
i avoid nesting calls. can me ?
regards
why want execute them in separate threads? them in single asynctask.
maybe take took @ threadpoolexecutor? used solve similar.
Comments
Post a Comment