Effective iOS Network Programming Techniques

17,624 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.

Published in: Technology
1 Comment
21 Likes
Statistics
Notes
No Downloads
Views
Total views
17,624
On SlideShare
0
From Embeds
0
Number of Embeds
589
Actions
Shares
0
Downloads
399
Comments
1
Likes
21
Embeds 0
No embeds

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
  • Effective iOS Network Programming Techniques

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

    ×