concurrent networking - made easy
DESCRIPTION
A short presentation we held at the Stockholm CocoaHeads meeting March 5th 2012 on concurrent networking in iOS.TRANSCRIPT
![Page 1: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/1.jpg)
CONCURRENT NETWORKING – MADE EASY!
CocoaHeads Stockholm, March 5 2012MARTHIN FREIJ / AMAZING APPLICATIONS
![Page 2: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/2.jpg)
![Page 3: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/3.jpg)
A two-man army of digital production veterans, on a crusade to delight people with pixel perfect design, delicate code and stunning user experiences.
We work hard to ensure that everything from concept and user interaction design to animations and technical implementation is as sleek as possible.
AMAZING APPLICATIONS / IN SHORT
![Page 4: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/4.jpg)
A tech savvy pixel pusher and design geek with over 15 years of experience working with global brands in digital channels. Also a confused father and a avid gamer.
CLIENT SHORT LIST: Vin & Sprit (Absolut Vodka & Malibu Rum), IKEA, Scania, Electrolux, Nokia, SCA (Libresse)
JIMMY POOPUU / ART DIRECTOR
Has written million lines of code as a software developer and held countless lectures as technical trainer during the past 10 years for clients in the bank, finance and media sector.
CLIENT SHORT LIST: ICA Banken, Handelsbanken, Bonnier, Dagens Nyheter, Dagens Industri
MARTHIN FREIJ / SENIOR DEVELOPER
![Page 5: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/5.jpg)
WKLY GREEN KITCHENNJUICE
![Page 6: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/6.jpg)
CONCURRENT NETWORKING – MADE EASY!
CocoaHeads Stockholm, March 5 2012MARTHIN FREIJ / AMAZING APPLICATIONS
![Page 7: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/7.jpg)
MADE EASY?IS IT HARD IN THE FIRST PLACE?
![Page 8: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/8.jpg)
APPLE PROVIDE SIMPLE API’S FOR NETWORKING
![Page 9: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/9.jpg)
NSURLConnectionNSURLRequestNSURLResponse
![Page 10: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/10.jpg)
FEW DEVELOPERS USE THEM
;(
![Page 11: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/11.jpg)
BECAUSE THEY REQUIRE A LOT OF BOILERPLATE CODE
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
![Page 12: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/12.jpg)
AND MAYBE YOU WANT TO DO MORE THAN ONE REQUEST
AT THE TIME?
![Page 13: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/13.jpg)
THEN YOU NEED TO KEEP TRACK
OF YOUR REQUESTS AND CONNECTIONS
![Page 14: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/14.jpg)
WE TEND TO USE BOILERPLATE CODE WRITTEN BY SOMEONE ELSE
![Page 15: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/15.jpg)
LIKE NETWORK LIBRARIES
![Page 16: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/16.jpg)
ASIHTTPREQUEST
![Page 17: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/17.jpg)
DOH! DISCONTINUED!
![Page 18: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/18.jpg)
AFNETWORKING
![Page 19: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/19.jpg)
BUGS!RACE CONDITIONS!MEMORY LEAKS!
!
![Page 20: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/20.jpg)
IT TURNS OUT TO BE KIND OF HARD
![Page 21: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/21.jpg)
NEW STUFF IN iOS 5
![Page 22: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/22.jpg)
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue*) queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler;
![Page 23: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/23.jpg)
NSURLRequestNSOperationQueueCompletion Handler
☞☞☞
![Page 24: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/24.jpg)
// Create the requestNSURL *url = [NSURL URLWithString:@"https://the.api.com/method/"];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// Create the queueNSOperationQueue *queue = [[NSOperationQueue alloc] init];queue.name = @"com.your.unique.queue.name";
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { // If there was an error getting the data if (error) { dispatch_async(dispatch_get_main_queue(), ^(void) { // Display error message in UI }); return; } // Do stuff with the data dispatch_async(dispatch_get_main_queue(), ^(void) { // Update UI });}];
![Page 25: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/25.jpg)
ONE MORE THING...
![Page 26: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/26.jpg)
CORE DATA AND CONCURRENCY HAVEN’T ALWAYS BEEN BEST BUDDIES
![Page 27: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/27.jpg)
UNTIL NOW IN iOS 5
![Page 28: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/28.jpg)
- (id)initWithConcurrencyType:(NSManagedObjectContextConcurrencyType)ct;- (void)setParentContext:(NSManagedObjectContext*)parent;- (void)performBlock:(void (^)())block;
NSManagedObjectContext
![Page 29: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/29.jpg)
Set your main context to execute on Main Queue(NSMainQueueConcurrencyType)
☞IMPORT DATA EXAMPLE (1 / 2)
Create an import context and tell Core Data to create a new queue for it (NSPrivateQueueConcurrencyType)
☞Set the main context as the import contexts parentContext☞
![Page 30: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/30.jpg)
On the import context, call performBlock and do the import (i.e. download data, validate it, import it, purge old data etc)
☞IMPORT DATA EXAMPLE (2 / 2)
Save changes on the import context. This will stage it up one level (to the main context)
☞Save changes on the main context. This will persist it on the associated persistent store (and update NSFetchedResultControllers etc)
☞
![Page 31: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/31.jpg)
// Setup the main context (probably in the AppDelegate)[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
![Page 32: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/32.jpg)
NSManagedObjectContext *importContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
importContext.parentContext = self.managedObjectContext;
[importContext performBlock:^{
// Download data, import etc..
NSError *importError = nil; [importContext save:&importError];
[importContext.parentContext performBlock:^{ NSError *parentError = nil; [importContext.parentContext save:&parentError]; }];}];
![Page 33: Concurrent networking - made easy](https://reader034.vdocument.in/reader034/viewer/2022052522/554a4797b4c905293a8b5601/html5/thumbnails/33.jpg)
THATS IT, THANKS FOR YOUR TIME!
www.amazing-apps.se