CONCURRENT NETWORKING     – MADE EASY!   CocoaHeads Stockholm, March 5 2012        MARTHIN FREIJ / AMAZING APPLICATIONS
AMAZING APPLICATIONS / IN SHORTA two-man army of digital productionveterans, on a crusade to delight peoplewith pixel perf...
MARTHIN FREIJ / SENIOR DEVELOPER             JIMMY POOPUU / ART DIRECTORHas written million lines of code as a       A tec...
NJUICE   WKLY   GREEN KITCHEN
CONCURRENT NETWORKING     – MADE EASY!   CocoaHeads Stockholm, March 5 2012        MARTHIN FREIJ / AMAZING APPLICATIONS
MADE EASY?IS IT HARDIN THE FIRST PLACE?
APPLE PROVIDE SIMPLEAPI’S FOR NETWORKING
NSURLConnectionNSURLRequestNSURLResponse
;(FEW DEVELOPERS USE THEM
BECAUSE THEY REQUIRE A LOT OF BOILERPLATE CODE- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate;- (void)...
AND MAYBE YOU WANT TO DO MORE THAN ONE REQUEST      AT THE TIME?
THEN YOU NEED TO   KEEP TRACKOF YOUR REQUESTSAND CONNECTIONS
WE TEND TO USE BOILERPLATE CODE    WRITTEN BY SOMEONE ELSE
LIKE NETWORK LIBRARIES
ASIHTTPREQUEST
DOH! DISCONTINUED!
AFNETWORKING
!     BUGS!RACE CONDITIONS! MEMORY LEAKS!
IT TURNS OUTTO BE KIND OF HARD
NEW STUFF IN iOS 5
+ (void)sendAsynchronousRequest:(NSURLRequest *)request              queue:(NSOperationQueue*) queue        completionHand...
☞ NSURLRequest☞ NSOperationQueue☞ Completion Handler
// Create the requestNSURL *url = [NSURL URLWithString:@"https://the.api.com/method/"];NSMutableURLRequest *request = [NSM...
ONE MORE THING...
CORE DATA AND CONCURRENCY HAVEN’T     ALWAYS BEEN BEST BUDDIES
UNTIL NOW IN iOS 5
NSManagedObjectContext- (id)initWithConcurrencyType:(NSManagedObjectContextConcurrencyType)ct;- (void)setParentContext:(NS...
IMPORT DATA EXAMPLE (1 / 2)☞   Set your main context to execute on Main Queue     (NSMainQueueConcurrencyType)☞   Create a...
IMPORT DATA EXAMPLE (2 / 2)☞   On the import context, call performBlock and do the import (i.e.    download data, validate...
// Setup the main context (probably in the AppDelegate)[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueue...
NSManagedObjectContext *importContext =  [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrency...
THATS IT, THANKS FOR YOUR TIME!       www.amazing-apps.se
Concurrent networking - made easy
Upcoming SlideShare
Loading in …5
×

Concurrent networking - made easy

2,490 views
2,415 views

Published on

A short presentation we held at the Stockholm CocoaHeads meeting March 5th 2012 on concurrent networking in iOS.

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,490
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
42
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Concurrent networking - made easy

  1. 1. CONCURRENT NETWORKING – MADE EASY! CocoaHeads Stockholm, March 5 2012 MARTHIN FREIJ / AMAZING APPLICATIONS
  2. 2. AMAZING APPLICATIONS / IN SHORTA two-man army of digital productionveterans, on a crusade to delight peoplewith pixel perfect design, delicate codeand stunning user experiences.We work hard to ensure that everythingfrom concept and user interaction designto animations and technicalimplementation is as sleek as possible.
  3. 3. MARTHIN FREIJ / SENIOR DEVELOPER JIMMY POOPUU / ART DIRECTORHas written million lines of code as a A tech savvy pixel pusher and design geeksoftware developer and held countless with over 15 years of experience workinglectures as technical trainer during the with global brands in digital channels.past 10 years for clients in the bank, Also a confused father and a avid gamer.finance and media sector. CLIENT SHORT LIST:CLIENT SHORT LIST: Vin & Sprit (Absolut Vodka & Malibu Rum), IKEA,ICA Banken, Handelsbanken, Bonnier, Dagens Scania, Electrolux, Nokia, SCA (Libresse)Nyheter, Dagens Industri
  4. 4. NJUICE WKLY GREEN KITCHEN
  5. 5. CONCURRENT NETWORKING – MADE EASY! CocoaHeads Stockholm, March 5 2012 MARTHIN FREIJ / AMAZING APPLICATIONS
  6. 6. MADE EASY?IS IT HARDIN THE FIRST PLACE?
  7. 7. APPLE PROVIDE SIMPLEAPI’S FOR NETWORKING
  8. 8. NSURLConnectionNSURLRequestNSURLResponse
  9. 9. ;(FEW DEVELOPERS USE THEM
  10. 10. 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;
  11. 11. AND MAYBE YOU WANT TO DO MORE THAN ONE REQUEST AT THE TIME?
  12. 12. THEN YOU NEED TO KEEP TRACKOF YOUR REQUESTSAND CONNECTIONS
  13. 13. WE TEND TO USE BOILERPLATE CODE WRITTEN BY SOMEONE ELSE
  14. 14. LIKE NETWORK LIBRARIES
  15. 15. ASIHTTPREQUEST
  16. 16. DOH! DISCONTINUED!
  17. 17. AFNETWORKING
  18. 18. ! BUGS!RACE CONDITIONS! MEMORY LEAKS!
  19. 19. IT TURNS OUTTO BE KIND OF HARD
  20. 20. NEW STUFF IN iOS 5
  21. 21. + (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue*) queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler;
  22. 22. ☞ NSURLRequest☞ NSOperationQueue☞ Completion Handler
  23. 23. // 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 });}];
  24. 24. ONE MORE THING...
  25. 25. CORE DATA AND CONCURRENCY HAVEN’T ALWAYS BEEN BEST BUDDIES
  26. 26. UNTIL NOW IN iOS 5
  27. 27. NSManagedObjectContext- (id)initWithConcurrencyType:(NSManagedObjectContextConcurrencyType)ct;- (void)setParentContext:(NSManagedObjectContext*)parent;- (void)performBlock:(void (^)())block;
  28. 28. IMPORT DATA EXAMPLE (1 / 2)☞ Set your main context to execute on Main Queue (NSMainQueueConcurrencyType)☞ 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
  29. 29. IMPORT DATA EXAMPLE (2 / 2)☞ On the import context, call performBlock and do the import (i.e. download data, validate it, import it, purge old data etc)☞ 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)
  30. 30. // Setup the main context (probably in the AppDelegate)[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
  31. 31. 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]; }];}];
  32. 32. THATS IT, THANKS FOR YOUR TIME! www.amazing-apps.se

×