ios - Issues related to calling UIKit methods from non-main thread -
i implemented login method in way:
[kvnprogress show]; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ //some error handling like: if ([_usernamefield.text length] < 4) { [kvnprogress showerrorwithstatus:@"username short!"]; _passwordfield.text = @""; return; } //then call login web service synchronously here: result = [serverrequests login]; dispatch_async(dispatch_get_main_queue(), ^{ if(!result) { [kvnprogress showerrorwithstatus:@"problem!" completion:null]; _passwordfield.text = @""; } else if([result.successful boolvalue]) { [kvnprogress showsuccesswithstatus:result.message]; } }); }); it crashed , surrounding blocks main queue (no priority default one) solved! problem is:kvnprogress showing in error handling area not next part call web service. it's not user friendly @ all! idea welcomed :)
you must call methods update user interface in way main thread, per the uikit documentation:
for part, use uikit classes only app’s main thread. particularly true classes derived uiresponder or involve manipulating app’s user interface in way.
i suggest try limit number of callbacks make main thread, therefore want batch user interface updates can.
then have do, correctly say, use dispatch_async callback main thread whenever need update ui, within background processing.
because it's asynchronous, won't interrupt background processing, , should have minimal interruption on main thread updating values on uikit components cheap, they'll update value , trigger setneedsdisplay they'll re-drawn @ next run loop.
from code, looks issue you're calling background thread:
if ([_usernamefield.text length] < 4) { [kvnprogress showerrorwithstatus:@"username short!"]; _passwordfield.text = @""; return; } this 100% ui updating code, , should therefore take place on main thread.
although, have no idea thread safety of kvnprogress, assume should called on main thread it's presenting error user.
your code therefore should something (assuming it's taking place on main thread begin with):
[kvnprogress show]; //some error handling like: if ([_usernamefield.text length] < 4) { [kvnprogress showerrorwithstatus:@"username short!"]; _passwordfield.text = @""; return; } dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ //then call login web service synchronously here: result = [serverrequests login]; dispatch_async(dispatch_get_main_queue(), ^{ if(!result) { [kvnprogress showerrorwithstatus:@"problem!" completion:null]; _passwordfield.text = @""; } else if([result.successful boolvalue]) { [kvnprogress showsuccesswithstatus:result.message]; } }); });
Comments
Post a Comment