c# - How to use a loading spinner for two async tasks consecutively -


currently have 2 independent async tasks making different web requests. when async task executed loading spinner shown. should add second async task first one, second depends on first.

my main problem loading spinner, while summing 2 async calls. there fadeout animation on hide() set 0.5 seconds. spinner shown 2 times short period time, looks kind of ugly. if skip animation flicker effect.

perhaps i'm wrong , should move normal synchronous requests, don't want block ui thread. don't know should search for. , simplified version:

public async void showlist() {     list<car> carlist = new list<car>();     carlist = await getcarlisttask();     list<string> manufacturers = extractids(carlist);     list<manufacturer> manufacturerlist = await getmanufacturerlisttask(manufacturers);     table.show(carlist, manufacturerlist); }  public async task<list<car>> getcarlisttask(){     loadingoverlay loadingoverlay = new loadingoverlay ();     list<car> carlist = new list<car>();      try{         loadingoverlay.show();         carlist = await task.run(() => manager.getcarlist());         loadingoverlay.hide();         return carlist;     }     catch(exception ex)     {         alert.show("something went wrong");         loadingoverlay.hide();     }      return carlist; }  public async task<list<manufacturer>> getmanufacturerlisttask(list<string> manufacturerid){     loadingoverlay loadingoverlay = new loadingoverlay ();     list<manufacturer> manufacturerlist = new list<manufacturer>();      try{         loadingoverlay.show();         manufacturerlist = await task.run(() => manager.getmanufacturerlist(manufacturerid));         loadingoverlay.hide();         return manufacturerlist;     }     catch(exception ex)     {         alert.show("something went wrong");         loadingoverlay.hide();     }      return manufacturerlist; } 

as can see loadingoverlay() created independently each request , depends on current view. show() in fact replacement this.navigationcontroller.view.insertsubviewbelow(loadingoverlay,this.navigationcontroller.navigationbar) , central place appdelegate, don't think has access on each navigation controller use (modal views, popover, ...).

how should solve issue?

edit:

currently i'm not displaying second loading spinner. interesting happens if user faster request ... should make second call synchronous?

another option come mind redesign loadingoverlay() in way flicker wouldn't obvious. nicest solution though have loading class displays loading spinner whole time until successive requests have been finished. don't know how such centralized instance like. simple solution summarize 2 calls 1 method, have aware of return type.

i don't see why can't move showing/hiding logic calling method:

public async task showlistsasync() {   loadingoverlay loadingoverlay = new loadingoverlay ();   loadingoverlay.show();   try   {     list<car> carlist = await task.run(() => manager.getcarlist());     list<string> manufacturers = extractids(carlist);     list<manufacturer> manufacturerlist = await task.run(() => manager.getmanufacturerlist(manufacturers));     table.show(carlist, manufacturerlist);   }   catch   {     alert.show("something went wrong");   }     {     loadingoverlay.hide();   } } 

perhaps example oversimplified , there's reason won't work.

also, side note:

i have 2 independent async tasks making different web requests

web requests asynchronous nature. if you're using httpclient, should simple expose asynchronous versions of getcarlist/getmanufacturerlist manager. remove need task.run:

list<car> carlist = await manager.getcarlistasync(); list<string> manufacturers = extractids(carlist); list<manufacturer> manufacturerlist = await manager.getmanufacturerlistasync(manufacturers); 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -