ios - Objective-C - What Types of Problems Can Occur When Updating UI Off Main Thread -


i had job interview morning , presented following code , asked find problem:

@interface ttwaitcontroller : uiviewcontroller  @property (strong, nonatomic) uilabel *alert;  @end  @implementation ttwaitcontroller  - (void)viewdidload {     cgrect frame = cgrectmake(20, 200, 200, 20);     self.alert = [[uilabel alloc] initwithframe:frame];     self.alert.text = @"please wait 10 seconds...";     self.alert.textcolor = [uicolor whitecolor];     [self.view addsubview:self.alert];      nsoperationqueue *waitqueue = [[nsoperationqueue alloc] init];     [waitqueue addoperationwithblock:^{         [nsthread sleepuntildate:[nsdate datewithtimeintervalsincenow:10]];         self.alert.text = @"thanks!";     }]; } 

this initiate ui updates off of main thread, no-no.

my question is, why, specifically? types of issues 1 expect when updating ui away main thread?

my question is, why, specifically?

because apple specifically tells us, on uiview reference page, that:

manipulations application’s user interface must occur on main thread. thus, should call methods of uiview class code running in main thread of application.

there's similar warning in core image programming guide:

cicontext , ciimage objects immutable, means each can shared safely among threads. multiple threads can use same gpu or cpu cicontext object render ciimage objects. however, not case cifilter objects, mutable. cifilter object cannot shared safely among threads. if app multithreaded, each thread must create own cifilter objects. otherwise, app behave unexpectedly.

and there's further clue in technical q&a qa1238, relates macos x applies ios well:

quartz thread safe on whole, individual quartz objects not. in general, can operate on object on thread long guarantee no 2 threads operating on same object simultaneously. easiest way achieve not share objects between threads.

so seems uiview not thread safe. view mutable object main thread needs access to, trying modify view different thread bound cause problems.

what types of issues 1 expect when updating ui away main thread?

imagine you're working object , data stored in object changed @ random times, , no warning. that's happens when multiple threads use mutable object without taking steps synchronize access object. can lead kinds of problems, outright crashes intermittent , hard find bugs.

setting text of label seems pretty innocuous, right? it's data: it's pretty unlikely 2 threads going try set label's text @ same time, , if seems worst might happen label contain garbled string. it's worse -- first, text property takes nsstring*, i.e. pointer, , if pointer gets garbled lead memory exception, crashing app. also, whenever set label's text, label schedules redrawing. put run loop or drawing system inconsistent state.

the list of bad things could happen if ignore advice limiting updates main thread impossible enumerate completely, , changes 1 ios release next. ultimately, don't need list -- need know how avoid problem.

limit ui updates main thread.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -