ios - Wait for subview to be displayed, then process, then remove subview -


i have spent week trying figure out how this.

what want display subview, http calls backend, , after remove subview.

... //display view [superview addsubview:blurredoverlay]; [superview bringsubviewtofront:blurredoverlay];  //after blurredoverlay displayed, try login user dispatch_group_t d_group = dispatch_group_create(); dispatch_queue_t bg_queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_group_async(d_group, bg_queue, ^{     //try login user     success = [self loginuser];     nslog(@"success=%i", success);     [nsthread sleepfortimeinterval:10.0]; //here force thread not return immediatly }); dispatch_group_wait(d_group, dispatch_time_forever);  //remove view after thread done processing [blurredoverlay removefromsuperview]; 

this not working. if have

[blurredoverlay removefromsuperview]; 

uncommented, blurredoverlay never display. if comment out, blurredovleray displayed can't remove it.

what need display blurredoverlay first, try login user (while blurredoverlay displayed), , after loginuser returns, remove blurred display.

you dispatching block asynchronous queue. main thread not stop wait until block completed. know already. that's why use dispatch groups block main thread until background task done.
problem approach ui refreshed after runloop completes current iteration. , not happen until method left.

this happens when code runs:

  • ui updated system
  • your method entered
    • add view
    • dispatch block
    • wait block complete
    • remove view
  • leave method
  • ui updated system

you see problem? ui not updated between adding view , removing it.

here should do. add view. dispatch block task runs in background. @ end of background block, dispatch block run when background task completed. block runs on main thread , removes view.

[superview addsubview:blurredoverlay]; dispatch_queue_t backgroundqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_async(backgroundqueue, ^{      // run web request here     [nsthread sleepfortimeinterval:10.0];      // task done     dispatch_async(dispatch_get_main_queue(), ^{         // ui updates have run on main thread, dispatch removal         [blurredoverlay removefromsuperview];     }); }); 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -