O'Reilly logo

The Core iOS Developer’s Cookbook, Fifth Edition by Rich Wardwell, Erica Sadun

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Recipe: Simple Downloads

Many classes provide convenience methods that allow you to request data from the Internet, wait until that data is received, and then move on to the next step in the application. The following snippet is both synchronous and blocking:

- (UIImage *)imageFromURLString:(NSString *)urlstring {     // This is a blocking call     return [UIImage imageWithData:[NSData         dataWithContentsOfURL:[NSURL URLWithString:urlstring]]]; }

You will not return from this method until all the data is received. If the connection hangs, so will your app. The iOS system watchdog will summarily terminate your app if it blocks the main thread for too long; it won’t just hang forever.

Do not use such convenience ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required