• Like
  • Save
Concurrent networking - made easy
Upcoming SlideShare
Loading in...5
×
 

Concurrent networking - made easy

on

  • 2,643 views

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

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

Statistics

Views

Total Views
2,643
Views on SlideShare
2,643
Embed Views
0

Actions

Likes
5
Downloads
41
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Hmm, that's an unfortunate characterization of AFNetworking. In reality, it's robust, thread-safe, free of leaks, and actively-maintained, and has been for a long time. Please consider updating your content, or reach out to me on Twitter (@mattt) if you'd like to discuss anything. Thanks!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Concurrent networking - made easy Concurrent networking - made easy Presentation Transcript

    • 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 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.
    • 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
    • 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)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
    • 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 completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler;
    • ☞ NSURLRequest☞ NSOperationQueue☞ Completion Handler
    • // 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 });}];
    • 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:(NSManagedObjectContext*)parent;- (void)performBlock:(void (^)())block;
    • 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
    • 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)
    • // Setup the main context (probably in the AppDelegate)[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    • 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]; }];}];
    • THATS IT, THANKS FOR YOUR TIME! www.amazing-apps.se