Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Effective Network Programming in iOS 7

Effective Network Programming in iOS 7

Presented at Mobile Developer Summit Oct 11th in Bangalore, India

Ben Scheirman

October 11, 2013
Tweet

More Decks by Ben Scheirman

Other Decks in Programming

Transcript

  1. Effective Network
    Programming
    with
    iOS 7
    Wednesday, October 9, 13

    View full-size slide

  2. Ben Scheirman
    CFOTDIFJSNBODPN !TVCEJHJUBM
    • Director

    View full-size slide

  3. Wednesday, October 9, 13

    View full-size slide

  4. Bite-sized Screencasts
    .com
    Weekly

    View full-size slide

  5. Why is this important?
    99%

    View full-size slide

  6. Why is this important?
    Doing

    View full-size slide

  7. Why is this important?
    Too

    View full-size slide

  8. Exhibit A: Stuttery Scroller
    Wednesday, October 9, 13

    View full-size slide

  9. Exhibit A: Stuttery Scroller
    Accessing

    View full-size slide

  10. Exhibit B: The Perpetual Loader
    Wednesday, October 9, 13

    View full-size slide

  11. Exhibit B: The Perpetual Loader
    How

    View full-size slide

  12. Exhibit C: Slow Loader
    Wednesday, October 9, 13

    View full-size slide

  13. Exhibit C: Slow Loader
    Loading

    View full-size slide

  14. Exhibit D: Yesterday’s News
    Wednesday, October 9, 13

    View full-size slide

  15. Exhibit D: Yesterday’s News
    Caching

    View full-size slide

  16. What we’ll cover
    w /463-4FTTJPO#BTJDT
    w -*7&$0%*/( XJTINFMVDL

    w "'/FUXPSLJOH
    w )551$BDIJOH
    w $BDIJOH1BUUFSOT
    w "1*%FTJHO5JQT
    Wednesday, October 9, 13

    View full-size slide

  17. NSURLConnection
    Invented for Safari ~2000, made
    public in 2003
    Poor separation of settings,
    config, cache
    Now replaced by NSURLSession
    classes
    Wednesday, October 9, 13

    View full-size slide

  18. NSURLConnection
    Invented for Safari ~2000, made
    public in 2003
    Poor separation of settings,
    config, cache
    Now replaced by NSURLSession
    classes
    Wednesday, October 9, 13

    View full-size slide

  19. NSURLSession
    Still use NSURLRequest
    Configurable Container (isolation w/ 3rd party libraries)
    Improved auth
    Rich Callbacks
    Wednesday, October 9, 13

    View full-size slide

  20. NSURLSession
    Wednesday, October 9, 13

    View full-size slide

  21. NSURLSession
    Wednesday, October 9, 13

    View full-size slide

  22. NSURLSession
    Wednesday, October 9, 13

    View full-size slide

  23. NSURLSession Class Family
    NSURLSessionConfiguration
    NSURLSessionTask
    NSURLSessionDelegate
    NSURLSession
    Wednesday, October 9, 13

    View full-size slide

  24. NSURLSession
    NSURLSession
    Interface for issuing network calls against a server
    Likely use case: 1 session per API
    Wednesday, October 9, 13

    View full-size slide

  25. NSURLSessionDelegate
    Delegate methods for a session:
    Used for connect level authentication challenges
    NTLM, Kerberos, Client certificates
    - URLSession:didBecomeInvalidWithError:
    - URLSession:didReceiveChallenge:completionHandler:
    Wednesday, October 9, 13

    View full-size slide

  26. NSURLSessionTask
    Wednesday, October 9, 13

    View full-size slide

  27. NSURLSessionTaskDelegate
    Delegate methods for a request:
    Only need to implement for fine-grained control
    - URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:
    - URLSession:task:didReceiveChallenge:completionHandler:
    - URLSession:task:needNewBodyStream:
    - URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:
    - URLSession:task:didCompleteWithError:
    Wednesday, October 9, 13

    View full-size slide

  28. NSURLSessionDataTaskDelegate
    Delegate methods for a data request:
    Only need to implement for fine-grained control
    - URLSession:dataTask:didReceiveChallenge:completionHandler:
    - URLSession:dataTask:didBecomeDownloadTask:
    - URLSession:dataTask:didReceiveData:
    - URLSession:dataTask:willCacheResponse:completionHandler:
    Wednesday, October 9, 13

    View full-size slide

  29. NSURLSessionDownloadDelegate
    - URLSession:downloadTask:didFinishDownloadingToURL:
    -URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:
    - URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes:
    Wednesday, October 9, 13

    View full-size slide

  30. Requesting Images
    Wednesday, October 9, 13

    View full-size slide

  31. Have you ever seen this?
    NSURL *imageUrl = [NSURL URLWithString:
    @”http://i.imgur.com/kwpjYwQ.jpg”];
    NSData *imageData = [NSData
    dataWithContentsOfURL:imageUrl];
    UIImage *image = [UIImage imageWithData:imageData];
    [imageView setImage:image];
    Wednesday, October 9, 13

    View full-size slide

  32. Have you ever seen this?
    NSURL *imageUrl = [NSURL URLWithString:
    @”http://i.imgur.com/kwpjYwQ.jpg”];
    NSData *imageData = [NSData
    dataWithContentsOfURL:imageUrl];
    UIImage *image = [UIImage imageWithData:imageData];
    [imageView setImage:image];
    WRONG
    Wednesday, October 9, 13

    View full-size slide

  33. Have you ever seen this?
    NSURL *imageUrl = [NSURL URLWithString:
    @”http://i.imgur.com/kwpjYwQ.jpg”];
    NSData *imageData = [NSData
    dataWithContentsOfURL:imageUrl];
    UIImage *image = [UIImage imageWithData:imageData];
    [imageView setImage:image];
    Wednesday, October 9, 13

    View full-size slide

  34. In case you were wondering...
    Wednesday, October 9, 13

    View full-size slide

  35. Demo
    •Demo: Download an image with
    NSURLSessionDownloadTask
    Wednesday, October 9, 13

    View full-size slide

  36. NSURLSessionDataTask
    •Represents GET/PUT/POST/DELETE
    •Handle completion callback
    •If error is present, request failed
    Wednesday, October 9, 13

    View full-size slide

  37. What constitutes an error?
    • Connection failed
    • Timeouts
    • Host invalid
    • Bad URL
    • Too many redirects
    • ... dozens more (check URL Loading System Error Codes)
    Wednesday, October 9, 13

    View full-size slide

  38. Demo
    •Demo: Searching the iTunes Store
    Wednesday, October 9, 13

    View full-size slide

  39. AFNetworking
    Wednesday, October 9, 13

    View full-size slide

  40. AFNetworking
    2.0
    Wednesday, October 9, 13

    View full-size slide

  41. Installation
    gem install cocoapods
    Podfile
    pod install
    platform :ios, "7.0"
    pod 'AFNetworking', '2.0'
    Wednesday, October 9, 13

    View full-size slide

  42. Installation
    gem install cocoapods
    Podfile
    pod install
    platform :ios, "7.0"
    pod 'AFNetworking', '2.0'
    Wednesday, October 9, 13

    View full-size slide

  43. Installation
    Podfile
    pod install
    platform :ios, "7.0"
    pod 'AFNetworking', '2.0'
    gem install cocoapods
    Wednesday, October 9, 13

    View full-size slide

  44. Installation
    Podfile
    pod install
    platform :ios, "6.0"
    pod 'AFNetworking'
    platform :ios, "7.0"
    pod 'AFNetworking', '2.0'
    gem install cocoapods
    Wednesday, October 9, 13

    View full-size slide

  45. AFNetworking 1.0 Design
    AFHTTPRequestOperation
    NSOperation
    AFURLConnectionOperation
    NSURLRequest
    NSURLConnection
    Wednesday, October 9, 13

    View full-size slide

  46. AFNetworking 2.0 Design
    AFHTTPSessionManager
    NSObject
    AFURLSessionManager NSURLSession
    NSURLSessionConfiguration
    NSURLSessionTask
    Wednesday, October 9, 13

    View full-size slide

  47. Serializers
    AFJSONSerializer
    AFURLSessionManager
    Wednesday, October 9, 13

    View full-size slide

  48. Usage
    Wednesday, October 9, 13

    View full-size slide

  49. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    Wednesday, October 9, 13

    View full-size slide

  50. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    Wednesday, October 9, 13

    View full-size slide

  51. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    Wednesday, October 9, 13

    View full-size slide

  52. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
    Wednesday, October 9, 13

    View full-size slide

  53. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
    initWithSessionConfiguration:config];
    Wednesday, October 9, 13

    View full-size slide

  54. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
    initWithSessionConfiguration:config];
    manager.responseSerializer = [[AFJSONSerializer alloc] init];
    Wednesday, October 9, 13

    View full-size slide

  55. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
    initWithSessionConfiguration:config];
    manager.responseSerializer = [[AFJSONSerializer alloc] init];
    [manager GET:url parameters:nil completion:^(NSURLSessionDataTask *task,
    Wednesday, October 9, 13

    View full-size slide

  56. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
    initWithSessionConfiguration:config];
    manager.responseSerializer = [[AFJSONSerializer alloc] init];
    [manager GET:url parameters:nil completion:^(NSURLSessionDataTask *task,
    id responseObject) {
    Wednesday, October 9, 13

    View full-size slide

  57. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
    initWithSessionConfiguration:config];
    manager.responseSerializer = [[AFJSONSerializer alloc] init];
    [manager GET:url parameters:nil completion:^(NSURLSessionDataTask *task,
    id responseObject) {
    // handle success
    Wednesday, October 9, 13

    View full-size slide

  58. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
    initWithSessionConfiguration:config];
    manager.responseSerializer = [[AFJSONSerializer alloc] init];
    [manager GET:url parameters:nil completion:^(NSURLSessionDataTask *task,
    id responseObject) {
    // handle success
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    Wednesday, October 9, 13

    View full-size slide

  59. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
    initWithSessionConfiguration:config];
    manager.responseSerializer = [[AFJSONSerializer alloc] init];
    [manager GET:url parameters:nil completion:^(NSURLSessionDataTask *task,
    id responseObject) {
    // handle success
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    // handle failure
    Wednesday, October 9, 13

    View full-size slide

  60. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration
    defaultConfiguration];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
    initWithSessionConfiguration:config];
    manager.responseSerializer = [[AFJSONSerializer alloc] init];
    [manager GET:url parameters:nil completion:^(NSURLSessionDataTask *task,
    id responseObject) {
    // handle success
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    // handle failure
    }];
    Wednesday, October 9, 13

    View full-size slide

  61. Requesting Images
    Wednesday, October 9, 13

    View full-size slide

  62. Requesting Images
    NSURL *imageUrl = [NSURL URLWithString:
    Wednesday, October 9, 13

    View full-size slide

  63. Requesting Images
    NSURL *imageUrl = [NSURL URLWithString:
    @”http://i.imgur.com/kwpjYwQ.jpg”];
    Wednesday, October 9, 13

    View full-size slide

  64. Requesting Images
    NSURL *imageUrl = [NSURL URLWithString:
    @”http://i.imgur.com/kwpjYwQ.jpg”];
    [imageView setImageWithURL:imageUrl];
    Wednesday, October 9, 13

    View full-size slide

  65. Requesting XML?
    Wednesday, October 9, 13

    View full-size slide

  66. Requesting XML?
    AFXMLResponseSerializer
    Wednesday, October 9, 13

    View full-size slide

  67. Requesting plist?
    Wednesday, October 9, 13

    View full-size slide

  68. Requesting plist?
    AFPropertyListResponseSerializer
    Wednesday, October 9, 13

    View full-size slide

  69. Requesting a custom data format?
    Wednesday, October 9, 13

    View full-size slide

  70. Requesting a custom data format?
    MyCustomResponseSerializer
    Wednesday, October 9, 13

    View full-size slide

  71. Demo: app.net client
    Wednesday, October 9, 13

    View full-size slide

  72. Other AFNetworking Goodies
    AFHTTPSessionManager setSecurityPolicy
    [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]
    Wednesday, October 9, 13

    View full-size slide

  73. Other AFNetworking Goodies
    UIActivityIndicatorView+AFNetworking.h
    [indicator setAnimatingWithStateOfTask:task]
    Wednesday, October 9, 13

    View full-size slide

  74. HTTP Status Code Cheat Sheet
    1xx - Not Used
    2xx - Thumbs Up
    3xx - It's over there
    4xx - You messed up
    5xx - We messed up
    Wednesday, October 9, 13

    View full-size slide

  75. HTTP Caching
    Client Server
    Wednesday, October 9, 13

    View full-size slide

  76. HTTP Caching
    Client Server
    GET /customer/152.json HTTP 1.1
    Host: example.com
    Wednesday, October 9, 13

    View full-size slide

  77. HTTP Caching
    Client Server
    HTTP/1.1 200 OK
    Content-Type: application/json
    Content-Length: 37142
    Etag:“e6482391249374127ca94128”
    Cache-Control:max-age=60, public
    Date:Sun, 21 Oct 2012 16:15:37
    GET /customer/152.json HTTP 1.1
    Host: example.com
    Wednesday, October 9, 13

    View full-size slide

  78. HTTP Caching
    GET /customer/152.json HTTP 1.1
    Host: example.com
    Client Server
    HTTP/1.1 200 OK
    Content-Type: application/json
    Content-Length: 37142
    Etag:“e6482391249374127ca94128”
    Cache-Control:max-age=60, public
    Date:Sun, 21 Oct 2012 16:15:37
    Wednesday, October 9, 13

    View full-size slide

  79. HTTP Caching
    GET /customer/152.json HTTP 1.1
    Host: example.com
    Client Server
    HTTP/1.1 200 OK
    Content-Type: application/json
    Content-Length: 37142
    Etag:“e6482391249374127ca94128”
    Cache-Control:max-age=60, public
    Date:Sun, 21 Oct 2012 16:15:37
    Wednesday, October 9, 13

    View full-size slide

  80. HTTP Caching
    GET /customer/152.json HTTP 1.1
    Host: example.com
    Client Server
    HTTP/1.1 200 OK
    Content-Type: application/json
    Content-Length: 37142
    Etag:“e6482391249374127ca94128”
    Cache-Control:max-age=60, public
    Date:Sun, 21 Oct 2012 16:15:37
    Wednesday, October 9, 13

    View full-size slide

  81. Leveraging Etag on the client
    Client Server
    Wednesday, October 9, 13

    View full-size slide

  82. Leveraging Etag on the client
    GET /customer/152.json HTTP 1.1
    Host: example.com
    If-None-Match:“e6482391249374127ca94128”
    Client Server
    Wednesday, October 9, 13

    View full-size slide

  83. Leveraging Etag on the client
    GET /customer/152.json HTTP 1.1
    Host: example.com
    If-None-Match:“e6482391249374127ca94128”
    Client Server
    HTTP/1.1 304 Not Modified
    < no response body >
    Wednesday, October 9, 13

    View full-size slide

  84. Leveraging Modified Date
    Client Server
    Wednesday, October 9, 13

    View full-size slide

  85. Leveraging Modified Date
    GET /customer/152.json HTTP 1.1
    Host: example.com
    If-Modified-Since:
    Client Server
    Wednesday, October 9, 13

    View full-size slide

  86. Leveraging Modified Date
    GET /customer/152.json HTTP 1.1
    Host: example.com
    If-Modified-Since:
    Client Server
    HTTP/1.1 304 Not Modified
    < no response body >
    Wednesday, October 9, 13

    View full-size slide

  87. Etag/Last Modified on the Server
    Wednesday, October 9, 13

    View full-size slide

  88. Etag/Last Modified on the Server
    def show
    Wednesday, October 9, 13

    View full-size slide

  89. Etag/Last Modified on the Server
    def show
    @band = Band.find(params[:id])
    Wednesday, October 9, 13

    View full-size slide

  90. Etag/Last Modified on the Server
    def show
    @band = Band.find(params[:id])
    fresh_when(:etag => @band,
    Wednesday, October 9, 13

    View full-size slide

  91. Etag/Last Modified on the Server
    def show
    @band = Band.find(params[:id])
    fresh_when(:etag => @band,
    :last_modified => @band,
    Wednesday, October 9, 13

    View full-size slide

  92. Etag/Last Modified on the Server
    def show
    @band = Band.find(params[:id])
    fresh_when(:etag => @band,
    :last_modified => @band,
    :public => true)
    Wednesday, October 9, 13

    View full-size slide

  93. Etag/Last Modified on the Server
    def show
    @band = Band.find(params[:id])
    fresh_when(:etag => @band,
    :last_modified => @band,
    :public => true)
    expires_in 10.minutes, :public => true
    Wednesday, October 9, 13

    View full-size slide

  94. Etag/Last Modified on the Server
    def show
    @band = Band.find(params[:id])
    fresh_when(:etag => @band,
    :last_modified => @band,
    :public => true)
    expires_in 10.minutes, :public => true
    end
    Wednesday, October 9, 13

    View full-size slide

  95. Etag/Last Modified on the Server
    def show
    @band = Band.find(params[:id])
    fresh_when(:etag => @band,
    :last_modified => @band,
    :public => true)
    expires_in 10.minutes, :public => true
    end
    Note: server still does a db query,
    but doesn’t render a template
    Saves on rendering & bandwidth for response transfer
    Wednesday, October 9, 13

    View full-size slide

  96. Cache-Control limitations
    Server
    Wednesday, October 9, 13

    View full-size slide

  97. Cache-Control limitations
    Server
    Wednesday, October 9, 13

    View full-size slide

  98. Cache-Control limitations
    Server
    Cache-Control: max-age: 600
    Wednesday, October 9, 13

    View full-size slide

  99. Cache-Control limitations
    Server
    Cache-Control: max-age: 600
    Wednesday, October 9, 13

    View full-size slide

  100. Cache-Control limitations
    Server
    Cache-Control: max-age: 600 Cache-Control: max-age: 600
    Wednesday, October 9, 13

    View full-size slide

  101. Reverse-Proxy Cache FTW
    Server
    Varnish / Squid / Nginx
    Wednesday, October 9, 13

    View full-size slide

  102. Reverse-Proxy Cache FTW
    Server
    Varnish / Squid / Nginx
    Wednesday, October 9, 13

    View full-size slide

  103. Reverse-Proxy Cache FTW
    Server
    Varnish / Squid / Nginx
    Wednesday, October 9, 13

    View full-size slide

  104. Reverse-Proxy Cache FTW
    Server
    Cache-Control: max-age: 600
    Varnish / Squid / Nginx
    Wednesday, October 9, 13

    View full-size slide

  105. Reverse-Proxy Cache FTW
    Server
    Cache-Control: max-age: 600
    Varnish / Squid / Nginx
    Cache-Control: max-age: 600
    Wednesday, October 9, 13

    View full-size slide

  106. Reverse-Proxy Cache FTW
    Server
    Cache-Control: max-age: 600
    Varnish / Squid / Nginx
    Cache-Control: max-age: 600
    Wednesday, October 9, 13

    View full-size slide

  107. Reverse-Proxy Cache FTW
    Server
    Cache-Control: max-age: 600
    Cache-Control: max-age: 600
    Varnish / Squid / Nginx
    Cache-Control: max-age: 600
    Wednesday, October 9, 13

    View full-size slide

  108. Leverage Cache-Control?
    HTTP/1.1 200 OK
    Content-Type: application/json
    Content-Length: 37142
    Etag:“e6482391249374127ca94128”
    Cache-Control:max-age=9999999999, public
    Date:Sun, 21 Oct 2012 16:15:37 GMT
    Wednesday, October 9, 13

    View full-size slide

  109. .JLF %PO`UFWFSDBMMNFBHBJO
    Wednesday, October 9, 13

    View full-size slide

  110. Pros / Cons of Cache-Control
    Client doesn’t wait for response
    Server spends no resources
    But, clients may render stale data
    Wednesday, October 9, 13

    View full-size slide

  111. Pros / Cons of Etag / IMS
    Server skips rendering
    Miniscule data transfer
    Appears instantaneous*
    But, server still doing a db query
    Wednesday, October 9, 13

    View full-size slide

  112. Reason about your data
    • List of US States
    • User’s Profile
    • Customer’s Order
    • Activity Timeline
    • Yesterday’s stats
    Wednesday, October 9, 13

    View full-size slide

  113. Tradeoff
    Fresh

    View full-size slide

  114. Caching w/ NSURLSession
    Leverages NSURLCache
    already
    Use default session configuration or customize cache
    settings
    Use ephemeral configuration to disable caching
    Wednesday, October 9, 13

    View full-size slide

  115. Caching w/ NSURLSession
    Customize if needed
    NSURLCache *cache =
    [[NSURLCache alloc] initWithMemoryCapacity:1024*1024*10
    diskCapacity:1024*1024*50
    diskPath:nil];
    [sessionConfiguration setURLCache:cache];
    Wednesday, October 9, 13

    View full-size slide

  116. Default Cache Location
    ~/Library/Caches/com.acme.app/Cache.db
    Wednesday, October 9, 13

    View full-size slide

  117. Wednesday, October 9, 13

    View full-size slide

  118. Switcheroo
    View Controller API Client
    Coordinator
    Wednesday, October 9, 13

    View full-size slide

  119. Switcheroo
    View Controller API Client
    Coordinator
    get data
    Wednesday, October 9, 13

    View full-size slide

  120. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    Wednesday, October 9, 13

    View full-size slide

  121. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    Wednesday, October 9, 13

    View full-size slide

  122. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    Wednesday, October 9, 13

    View full-size slide

  123. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    Wednesday, October 9, 13

    View full-size slide

  124. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    200 OK
    Wednesday, October 9, 13

    View full-size slide

  125. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    200 OK
    (parsed response)
    Wednesday, October 9, 13

    View full-size slide

  126. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    200 OK
    (parsed response)
    update cache
    Wednesday, October 9, 13

    View full-size slide

  127. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    200 OK
    (parsed response)
    (parsed response)
    update cache
    Wednesday, October 9, 13

    View full-size slide

  128. API Design Tips
    Don’t expose your internal model
    http://www.myspace.com/na6_build/photos/20613762
    Wednesday, October 9, 13

    View full-size slide

  129. API Design Tips
    Version your API
    Wednesday, October 9, 13

    View full-size slide

  130. API Design Tips
    Send OS Version, App Version,
    and Device Name as headers
    Device Name
    OS Version
    App Version
    Wednesday, October 9, 13

    View full-size slide

  131. API Design Tips
    Have a way to notify clients
    to upgrade (even force)
    Wednesday, October 9, 13

    View full-size slide

  132. API Design Tips
    • Techniques for permeating deletes
    • Valid ID Check
    • Client requests changes, server returns a set of new
    records, changed records, and deleted record ids
    Wednesday, October 9, 13

    View full-size slide

  133. API Design Tips
    Page Large Datasets
    Wednesday, October 9, 13

    View full-size slide

  134. API Design Tips
    Build-in HTTP Caching support
    from day 1
    Wednesday, October 9, 13

    View full-size slide

  135. API Design Tips
    Take care when caching
    user-specific data
    Wednesday, October 9, 13

    View full-size slide

  136. API Design Tips
    Turn on Gzip Compression
    Wednesday, October 9, 13

    View full-size slide

  137. API Design Tips
    Measure your API response
    times
    Wednesday, October 9, 13

    View full-size slide

  138. Thank you!
    Ben Scheirman
    @subdigital
    @bens (adn)
    @nsscreencast
    Wednesday, October 9, 13

    View full-size slide