• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Effective iOS Network Programming Techniques
 

Effective iOS Network Programming Techniques

on

  • 16,282 views

I gave this talk at iPhone Dev Con in Boston, April 2011. It covers making HTTP requests using ASIHTTPRequest, UITableViews with remote images, caching patterns, and API design tips.

I gave this talk at iPhone Dev Con in Boston, April 2011. It covers making HTTP requests using ASIHTTPRequest, UITableViews with remote images, caching patterns, and API design tips.

Statistics

Views

Total Views
16,282
Views on SlideShare
15,768
Embed Views
514

Actions

Likes
21
Downloads
351
Comments
1

13 Embeds 514

http://benscheirman.com 235
http://www.elobyte.de 84
http://www.codemav.com 83
http://flux88.com 58
http://elobyte.de 41
http://blog.dev 3
http://webcache.googleusercontent.com 3
http://www.verious.com 2
url_unknown 1
http://zootool.com 1
http://admin 1
http://www.slideshare.net 1
http://www.benscheirman.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

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 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • thank you
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Effective iOS Network Programming Techniques Effective iOS Network Programming Techniques Presentation Transcript

  • Effective Network Programming Techniques for Ios i P h o n e D e v C o n B o s t o n 2 0 1 1
  • Ben ScheirmanDirector of DevelopmentChaiONEHouston, TXben@scheirman.com@subdigitalhttp://flux88.com
  • Why is this important?99% of the apps we write are not self-containedDoing it wrong is terrible for the user’sexperience
  • Example 1:The Stuttery Scroller
  • Example 1:The Stuttery Scroller Accessing netw ork on the main thread
  • Example 2:The Perpetual Loader
  • Example 2:The Perpetual Loader ... oug h data cach ing enNot
  • The Slow Loader
  • The Slow Loader Loading too mu ch data...
  • Example 3:Stale Toast
  • Example 3: Stale Toast ow up ta do esnt shNew da ht away... rig
  • What we’ll coverASIHTTPRequestUITableView with ProgressTrackingremote images download/upload progressLeveraging HTTP API Design TipsCaching PatternsLarge File Transfer
  • ASI HTTP Requesthttp://github.com/pokeb/asihttprequest
  • InstallationCopy Files into your projectCopy ReachabilityLink in Frameworks
  • Usage
  • Usage#import “ASIHTTPRequest.h”
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers {
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...];
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”];
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self];
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];}
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];}- (void)requestDidComplete:(ASIHTTPRequest *)request {
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];}- (void)requestDidComplete:(ASIHTTPRequest *)request { //when requesting text (xml, json, etc)
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];}- (void)requestDidComplete:(ASIHTTPRequest *)request { //when requesting text (xml, json, etc) NSString *body = [request responseString];
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];}- (void)requestDidComplete:(ASIHTTPRequest *)request { //when requesting text (xml, json, etc) NSString *body = [request responseString]; //when requesting binary data
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];}- (void)requestDidComplete:(ASIHTTPRequest *)request { //when requesting text (xml, json, etc) NSString *body = [request responseString]; //when requesting binary data NSData *data = [request responseData];
  • Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];}- (void)requestDidComplete:(ASIHTTPRequest *)request { //when requesting text (xml, json, etc) NSString *body = [request responseString]; //when requesting binary data NSData *data = [request responseData];}
  • UITableViewLoading images for each rowDont make blocking network calls intableView:cellForRowAtIndexPath:Leverage NSOperationQueue & a local cacheFetch images in the background, reload the rowin question
  • Leveraging HTTP Request Response 200 OKGET /resource {resource:"hello"}GET /resource 304 NOT MODIFIEDIf-Modified-Since: 03-16-2010 16:10:00 200 OK E-Tag: 000ea9512912GET /resource {resource:"hello"}GET /resource 304 NOT MODIFIEDIf-None-Match: 000ea9512912
  • CachingFresh Data User Experience
  • CachingFresh Data User Experience
  • CachingCache Storage Policy Permanent SessionCache Policy ASIAskServerIfModifiedWhenStaleCachePolicy ASIAskServerIfModifiedCachePolicy ASIOnlyLoadIfNotCachedCachePolicy ASIDontLoadCachePolicy ASIFallbackToCacheIfLoadFailsCachePolicy
  • Enabling the Cache[ASIHTTPRequest setDownloadCache:[ASIDownloadCache sharedChache]];//or per request[request setDownloadCache:[ASIDownloadCache sharedCache]];
  • Checking if Content exists in the cache#import "ASIHTTPRequest.h"#import "ASIDownloadCache.h"- (void)fetchImage { NSURL *url = ... NSData *cacheData = [[ASIDownloadCache sharedCache] cachedDataForUrl:url]; //do something with cachedData}#import "ASIHTTPRequest.h"#import "ASIDownloadCache.h"- (void)requestDidFinish(ASIHTTPRequest *)request { if ([request didUseCachedResponse]) { //response was served from the cache! }}
  • Tracking ProgressSet the upload/download progress delegate ofa request smart enough to bind directly to a UIProgressView For large downloads, turn on showAccurateProgress in order to get better precision over progress updates
  • Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk
  • Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”
  • Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”- (void)fetchDatabase {
  • Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”- (void)fetchDatabase { NSURL *url = [NSURL URLwithString:...];
  • Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”- (void)fetchDatabase { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  • Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”- (void)fetchDatabase { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self];
  • Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”- (void)fetchDatabase { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request setDownloadDestinationPath:...];
  • Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”- (void)fetchDatabase { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request setDownloadDestinationPath:...]; [request startAsynchronous];
  • Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”- (void)fetchDatabase { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request setDownloadDestinationPath:...]; [request startAsynchronous];}
  • Tracking progressof multiple requests ASINetworkQueue requests are NSOperations, just add them need to explicitly start the queue will perform a HEAD request for each to calculate total size reports progress similar to ASIHTTPRequest
  • API DESIGN TIPSDont couple your API to your internal server model
  • API DESIGN TIPSBuild in Versioning
  • API DESIGN TIPSRecord Client Device Info Device Name OS Version App Version
  • API DESIGN TIPSProvide News alertsEnforce Minimum App Version "Please update to 1.2 for improved functionality"
  • API DESIGN TIPSSupport E-Tags and/or If-Modified-SinceHeaderProvide date & paging filters on large datasets
  • API DESIGN TIPSTechniques for permeating deletes Client supplies list of ids, server returns list of valid ones Client requests changes, server returns a set of new records, changed records, and deleted record ids (requires server to support soft-delete of archive tables)
  • API DESIGN TIPSAggressively cache (where possible) publicendpointsAvoid caching per-user information unlessyou have a limited set of users (or unlimitedmemory on your server)
  • Questions?