ios - What's happen if control UI by performSelectorInBackground? -
i read informations ui interface update on mainthread.
i need asynchronous update uibutton
s, use performselectorinbackground
, it's work fine on simulator , device (ipad4).
[self performselectorinbackground:@selector(toggletoupperwhensingleshift) withobject:nil]; - (void)toggletoupperwhensingleshift{ shiftstateindicator = 1; (uiview *apad in self.subviews) { if ( [apad iskindofclass:[uibutton class]] ) { uibutton *abutpad = (uibutton *)apad; nsmutablestring *currenttitle = [nsmutablestring stringwithstring:[abutpad titleforstate:uicontrolstatenormal]]; nsstring *firstchar = [currenttitle substringtoindex:1]; [currenttitle replacecharactersinrange:nsmakerange(0, 1) withstring:[firstchar uppercasestring]]; [abutpad settitle:currenttitle forstate:uicontrolstatenormal]; [abutpad settitle:currenttitle forstate:uicontrolstatehighlighted]; currenttitle = [nsmutablestring stringwithstring:[abutpad titleforstate:uicontrolstateselected]]; firstchar = [currenttitle substringtoindex:1]; [currenttitle replacecharactersinrange:nsmakerange(0, 1) withstring:[firstchar uppercasestring]]; [abutpad settitle:currenttitle forstate:uicontrolstateselected]; } } }
i'm worried unwanted functions happen if keep code. can explain me detail performselectorinbackground
?
why not use update ui , why it's ok app? anyway debug problem appreciate!
performselectorinbackground:
never want, (and not since creation of gcd). creates new thread have little control over. thread run long method dispatch it.
by "little control" mean don't nsthread
object back, easy accidentally call method many times , fork unbounded number of threads. i've seen happen in several programs.
in ios, should never manually create thread. gcd , nsoperation
handle manual threads do, better. typically want thread pooling don't spin , spin down threads time. gcd gives that. want cap how many threads create don't overwhelm processor. gcd gives that. want able prioritize background actions easily. gcd gives that, too.
all said, can't figure out why you're trying above operation on background thread. work ui updates. must never, ever, try modify ui on background thread. undefined behavior, , when goes wrong, goes wrong. fact it's worked in few cases means nothing. uikit not thread safe. should call toggletoupperwhensingleshift
on main thread. don't see in should block you, , overhead of context switching background thread isn't worth here (even if safe, it's not).
Comments
Post a Comment