2

As pointed out by bbum here, the doc says: "For the most part, UIKit classes should only be used from the main thread of an application This is especially true for derived classes UIResponder or involve the manipulation of user interface of your application in any way. ".

I thought I understood that the methods of drawings could not be called in a background thread, so that the creation could be done in the background, as the drawRect method is only called when the view is added. But maybe i am wrong.

In summary, does that this kind of code is risky?

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

            dispatch_async(queue, ^{

                NSString *fileName = [pathToModel  stringByAppendingPathComponent:[[compDico valueForKey:@"fileName"] lastPathComponent]];

                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:
                                                                             fileName]];
                UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 62, 190, 20)];
                [label setText:[[someArray objectAtIndex:i-1] someText]];
                [label setNumberOfLines:0];
                label.font=[UIFont fontWithName:@"arial" size:10.0f];
                [label setBackgroundColor:[UIColor clearColor]];

                // Create some other view here
                // ...

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.view addSubview:imageView];
                    [self.view addSubview:label];
                    //Add other view here
                    // ...
                });
            });

Thanks for in advance your responses!

Community
  • 1
  • 1
Alban
  • 1,624
  • 11
  • 21
  • There is some good info here: http://stackoverflow.com/questions/16299842/uikit-and-gcd-thread-safety?rq=1 – rmaddy Aug 27 '13 at 22:36
  • 3
    If it is documented as being unsupported, than it isn't supported. *Works by coincidence* is not an acceptable approach to concurrency. It may "work" in the situations you've tested, but then fail after the next software update, fail on some piece of hardware you don't have, or on some customer configuration that you've never tested. (Thanks for making an explicit question, btw) – bbum Aug 27 '13 at 23:02
  • Separate and slightly irrelevant question regarding the OP's question. @bbum will know the answer, sorry for posting it here, but didn't feel like it deserved its own question. What does the '0ul' mean/convey in the creation of the dispatch queue? – max_ Aug 27 '13 at 23:43
  • @max_ '0ul' (or '0UL') is a numeric constant of type `unsigned long` with the value zero. The second parameter of `dispatch_get_global_queue` is currently unused, so docs say you should simply pass zero. – jlehr Aug 27 '13 at 23:48
  • @jlehr thanks for summing that up! – max_ Aug 27 '13 at 23:50

1 Answers1

4

Yes, this is risky. How risky it is only Apple developers can say.

If the documentation says "don't use it", just don't use it.

Note that many UI objects can (and do) use shared resources. If you use them in a background thread, you'll get a race condition on the shared resource and anything can happen.

Sulthan
  • 128,090
  • 22
  • 218
  • 270