Your SlideShare is downloading. ×
Effective iOS Network Programming Techniques
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Effective iOS Network Programming Techniques

16,638
views

Published on

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.

Published in: Technology

1 Comment
20 Likes
Statistics
Notes
No Downloads
Views
Total Views
16,638
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
382
Comments
1
Likes
20
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \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
  • Transcript

    • 1. 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
    • 2. Ben ScheirmanDirector of DevelopmentChaiONEHouston, TXben@scheirman.com@subdigitalhttp://flux88.com
    • 3. Why is this important?99% of the apps we write are not self-containedDoing it wrong is terrible for the user’sexperience
    • 4. Example 1:The Stuttery Scroller
    • 5. Example 1:The Stuttery Scroller Accessing netw ork on the main thread
    • 6. Example 2:The Perpetual Loader
    • 7. Example 2:The Perpetual Loader ... oug h data cach ing enNot
    • 8. The Slow Loader
    • 9. The Slow Loader Loading too mu ch data...
    • 10. Example 3:Stale Toast
    • 11. Example 3: Stale Toast ow up ta do esnt shNew da ht away... rig
    • 12. What we’ll coverASIHTTPRequestUITableView with ProgressTrackingremote images download/upload progressLeveraging HTTP API Design TipsCaching PatternsLarge File Transfer
    • 13. ASI HTTP Requesthttp://github.com/pokeb/asihttprequest
    • 14. InstallationCopy Files into your projectCopy ReachabilityLink in Frameworks
    • 15. Usage
    • 16. Usage#import “ASIHTTPRequest.h”
    • 17. Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers {
    • 18. Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...];
    • 19. Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    • 20. Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”];
    • 21. Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self];
    • 22. Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];
    • 23. Usage#import “ASIHTTPRequest.h”- (void)fetchCustomers { NSURL *url = [NSURL URLwithString:...]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setMethod:@”GET”]; [request setDelegate:self]; [request startAsynchronous];}
    • 24. 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 {
    • 25. 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)
    • 26. 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];
    • 27. 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
    • 28. 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];
    • 29. 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];}
    • 30. UITableViewLoading images for each rowDont make blocking network calls intableView:cellForRowAtIndexPath:Leverage NSOperationQueue & a local cacheFetch images in the background, reload the rowin question
    • 31. 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
    • 32. CachingFresh Data User Experience
    • 33. CachingFresh Data User Experience
    • 34. CachingCache Storage Policy Permanent SessionCache Policy ASIAskServerIfModifiedWhenStaleCachePolicy ASIAskServerIfModifiedCachePolicy ASIOnlyLoadIfNotCachedCachePolicy ASIDontLoadCachePolicy ASIFallbackToCacheIfLoadFailsCachePolicy
    • 35. Enabling the Cache[ASIHTTPRequest setDownloadCache:[ASIDownloadCache sharedChache]];//or per request[request setDownloadCache:[ASIDownloadCache sharedCache]];
    • 36. 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! }}
    • 37. 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
    • 38. Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk
    • 39. Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”
    • 40. Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”- (void)fetchDatabase {
    • 41. Uploading/Downloading Large FilesReduce memory overhead by streamingdirectly to/from disk#import “ASIHTTPRequest.h”- (void)fetchDatabase { NSURL *url = [NSURL URLwithString:...];
    • 42. 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];
    • 43. 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];
    • 44. 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:...];
    • 45. 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];
    • 46. 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];}
    • 47. 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
    • 48. API DESIGN TIPSDont couple your API to your internal server model
    • 49. API DESIGN TIPSBuild in Versioning
    • 50. API DESIGN TIPSRecord Client Device Info Device Name OS Version App Version
    • 51. API DESIGN TIPSProvide News alertsEnforce Minimum App Version "Please update to 1.2 for improved functionality"
    • 52. API DESIGN TIPSSupport E-Tags and/or If-Modified-SinceHeaderProvide date & paging filters on large datasets
    • 53. 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)
    • 54. API DESIGN TIPSAggressively cache (where possible) publicendpointsAvoid caching per-user information unlessyou have a limited set of users (or unlimitedmemory on your server)
    • 55. Questions?