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

Effective Network Programming w/ AFNetworking

Effective Network Programming w/ AFNetworking

Very few apps these days are self-contained silos. More often than not, your app will need to consume data from external sources. In this session, you'll learn how to properly write networking code so that you don't block threads, don't fetch data you don't need, and cache the right data the right way. You'll learn about an excellent framework called AFNetworking to simplify your networking code. And you'll also see some practical examples such as fetching images asynchronously to update a UITableView.

Ben Scheirman

October 26, 2012
Tweet

More Decks by Ben Scheirman

Other Decks in Programming

Transcript

  1. Effective Network
    Programming
    with
    Wednesday, October 24, 12

    View full-size slide

  2. Ben Scheirman
    CFOTDIFJSNBODPN !TVCEJHJUBM
    • Director

    View full-size slide

  3. Bite-sized Screencasts
    .com
    Wednesday, October 24, 12

    View full-size slide

  4. Why is this important?
    99%

    View full-size slide

  5. Why is this important?
    Doing

    View full-size slide

  6. Why is this important?
    Everyone

    View full-size slide

  7. Why is this important?
    Everyone

    View full-size slide

  8. Exhibit A: Stuttery Scroller
    Wednesday, October 24, 12

    View full-size slide

  9. Exhibit A: Stuttery Scroller
    Accessing

    View full-size slide

  10. Exhibit B: The Perpetual Loader
    Wednesday, October 24, 12

    View full-size slide

  11. Exhibit B: The Perpetual Loader
    How

    View full-size slide

  12. Exhibit C: Slow Loader
    Wednesday, October 24, 12

    View full-size slide

  13. Exhibit C: Slow Loader
    Loading

    View full-size slide

  14. Exhibit D: Yesterday’s News
    Wednesday, October 24, 12

    View full-size slide

  15. Exhibit D: Yesterday’s News
    Caching

    View full-size slide

  16. What we’ll cover
    w"'/FUXPSLJOH#BTJDT
    w-*7&$0%*/( XJTINFMVDL

    w)551$BDIJOH
    w$BDIJOH1BUUFSOT
    w"1*%FTJHO5JQT
    Wednesday, October 24, 12

    View full-size slide

  17. AFNetworking
    Wednesday, October 24, 12

    View full-size slide

  18. Installation
    gem install cocoapods
    Podfile
    pod install
    platform :ios, “5.0”
    pod ‘AFNetworking’
    Wednesday, October 24, 12

    View full-size slide

  19. Installation
    gem install cocoapods
    Podfile
    pod install
    platform :ios, “5.0”
    pod ‘AFNetworking’
    Wednesday, October 24, 12

    View full-size slide

  20. Installation
    gem install cocoapods
    Podfile
    platform :ios, “5.0”
    pod ‘AFNetworking’
    pod install
    Wednesday, October 24, 12

    View full-size slide

  21. Installation
    gem install cocoapods
    Podfile
    pod install
    platform :ios, “5.0”
    pod ‘AFNetworking’
    Wednesday, October 24, 12

    View full-size slide

  22. AFNetworking Design
    AFHTTPRequestOperation
    NSOperation
    AFURLConnectionOperation
    NSURLRequest
    NSURLConnection
    Wednesday, October 24, 12

    View full-size slide

  23. Why NSOperation?
    •Queues

    View full-size slide

  24. Usage
    Wednesday, October 24, 12

    View full-size slide

  25. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    Wednesday, October 24, 12

    View full-size slide

  26. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    Wednesday, October 24, 12

    View full-size slide

  27. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    Wednesday, October 24, 12

    View full-size slide

  28. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    operation = [[AFHTTPRequestOperation alloc]
    Wednesday, October 24, 12

    View full-size slide

  29. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    operation = [[AFHTTPRequestOperation alloc]
    initWithRequest:request];
    Wednesday, October 24, 12

    View full-size slide

  30. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    operation = [[AFHTTPRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(AFHTTPRequestOperation *operation,
    Wednesday, October 24, 12

    View full-size slide

  31. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    operation = [[AFHTTPRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(AFHTTPRequestOperation *operation,
    id responseObject) {
    Wednesday, October 24, 12

    View full-size slide

  32. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    operation = [[AFHTTPRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(AFHTTPRequestOperation *operation,
    id responseObject) {
    // handle success
    Wednesday, October 24, 12

    View full-size slide

  33. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    operation = [[AFHTTPRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(AFHTTPRequestOperation *operation,
    id responseObject) {
    // handle success
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    Wednesday, October 24, 12

    View full-size slide

  34. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    operation = [[AFHTTPRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(AFHTTPRequestOperation *operation,
    id responseObject) {
    // handle success
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // handle failure
    Wednesday, October 24, 12

    View full-size slide

  35. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    operation = [[AFHTTPRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(AFHTTPRequestOperation *operation,
    id responseObject) {
    // handle success
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // handle failure
    }];
    Wednesday, October 24, 12

    View full-size slide

  36. Usage
    NSURL *url = [NSURL URLWithString:@”http://google.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation;
    operation = [[AFHTTPRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(AFHTTPRequestOperation *operation,
    id responseObject) {
    // handle success
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // handle failure
    }];
    [operation start];
    Wednesday, October 24, 12

    View full-size slide

  37. Requesting JSON
    Wednesday, October 24, 12

    View full-size slide

  38. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    Wednesday, October 24, 12

    View full-size slide

  39. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    Wednesday, October 24, 12

    View full-size slide

  40. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation;
    Wednesday, October 24, 12

    View full-size slide

  41. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation;
    operation = [[AFJSONRequestOperation alloc]
    Wednesday, October 24, 12

    View full-size slide

  42. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation;
    operation = [[AFJSONRequestOperation alloc]
    initWithRequest:request];
    Wednesday, October 24, 12

    View full-size slide

  43. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation;
    operation = [[AFJSONRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(
    Wednesday, October 24, 12

    View full-size slide

  44. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation;
    operation = [[AFJSONRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(
    AFHTTPRequestOperation *operation, id responseObject) {
    Wednesday, October 24, 12

    View full-size slide

  45. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation;
    operation = [[AFJSONRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(
    AFHTTPRequestOperation *operation, id responseObject) {
    // responseObject is already parsed
    Wednesday, October 24, 12

    View full-size slide

  46. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation;
    operation = [[AFJSONRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(
    AFHTTPRequestOperation *operation, id responseObject) {
    // responseObject is already parsed
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    Wednesday, October 24, 12

    View full-size slide

  47. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation;
    operation = [[AFJSONRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(
    AFHTTPRequestOperation *operation, id responseObject) {
    // responseObject is already parsed
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // handle failure
    Wednesday, October 24, 12

    View full-size slide

  48. Requesting JSON
    NSURL *url = [NSURL URLWithString:@”http://jsonip.com”];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation;
    operation = [[AFJSONRequestOperation alloc]
    initWithRequest:request];
    [operation setCompletionWithSuccess:^(
    AFHTTPRequestOperation *operation, id responseObject) {
    // responseObject is already parsed
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // handle failure
    }];
    Wednesday, October 24, 12

    View full-size slide

  49. Requesting Images
    Wednesday, October 24, 12

    View full-size slide

  50. Requesting Images
    NSURL *imageUrl = [NSURL URLWithString:
    Wednesday, October 24, 12

    View full-size slide

  51. Requesting Images
    NSURL *imageUrl = [NSURL URLWithString:
    @”http://i.imgur.com/RH1Si.jpg”];
    Wednesday, October 24, 12

    View full-size slide

  52. Requesting Images
    NSURL *imageUrl = [NSURL URLWithString:
    @”http://i.imgur.com/RH1Si.jpg”];
    [imageView setImageWithURL:imageUrl];
    Wednesday, October 24, 12

    View full-size slide

  53. Say it with me now...
    Wednesday, October 24, 12

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  57. Requesting XML?
    Wednesday, October 24, 12

    View full-size slide

  58. Requesting XML?
    AFXMLRequestOperation
    Wednesday, October 24, 12

    View full-size slide

  59. Requesting plist?
    Wednesday, October 24, 12

    View full-size slide

  60. Requesting plist?
    AFPropertyListRequestOperation
    Wednesday, October 24, 12

    View full-size slide

  61. Requesting raw data?
    Wednesday, October 24, 12

    View full-size slide

  62. Requesting raw data?
    AFHTTPRequestOperation
    Wednesday, October 24, 12

    View full-size slide

  63. Demo: app.net client
    Wednesday, October 24, 12

    View full-size slide

  64. What is success?
    200-299
    Wednesday, October 24, 12

    View full-size slide

  65. What about 404?
    AFNetworkingErrorDomain
    (check response code)
    calls failure block
    Wednesday, October 24, 12

    View full-size slide

  66. What about 302?
    call setRedirectResponseBlock:
    to alter behavior
    transparently handled for you
    Wednesday, October 24, 12

    View full-size slide

  67. Other errors
    Timeout
    No network available
    401 Unauthorized
    403 Forbidden
    5xx server error
    Wednesday, October 24, 12

    View full-size slide

  68. HTTP Caching
    Client Server
    Wednesday, October 24, 12

    View full-size slide

  69. HTTP Caching
    Client Server
    GET /customer/152.json HTTP 1.1
    Host: example.com
    Wednesday, October 24, 12

    View full-size slide

  70. 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 24, 12

    View full-size slide

  71. 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 24, 12

    View full-size slide

  72. 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 24, 12

    View full-size slide

  73. 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 24, 12

    View full-size slide

  74. Leveraging Etag on the client
    Client Server
    Wednesday, October 24, 12

    View full-size slide

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

    View full-size slide

  76. 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 24, 12

    View full-size slide

  77. Leveraging Modified Date
    Client Server
    Wednesday, October 24, 12

    View full-size slide

  78. Leveraging Modified Date
    GET /customer/152.json HTTP 1.1
    Host: example.com
    If-Modified-Since:
    Client Server
    Wednesday, October 24, 12

    View full-size slide

  79. 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 24, 12

    View full-size slide

  80. Etag/Last Modified on the Server
    Wednesday, October 24, 12

    View full-size slide

  81. Etag/Last Modified on the Server
    def show
    Wednesday, October 24, 12

    View full-size slide

  82. Etag/Last Modified on the Server
    def show
    @customer = Customer.find(params[:id])
    Wednesday, October 24, 12

    View full-size slide

  83. Etag/Last Modified on the Server
    def show
    @customer = Customer.find(params[:id])
    fresh_when(:etag => @customer,
    Wednesday, October 24, 12

    View full-size slide

  84. Etag/Last Modified on the Server
    def show
    @customer = Customer.find(params[:id])
    fresh_when(:etag => @customer,
    :last_modified => @customer,
    Wednesday, October 24, 12

    View full-size slide

  85. Etag/Last Modified on the Server
    def show
    @customer = Customer.find(params[:id])
    fresh_when(:etag => @customer,
    :last_modified => @customer,
    :public => true)
    Wednesday, October 24, 12

    View full-size slide

  86. Etag/Last Modified on the Server
    def show
    @customer = Customer.find(params[:id])
    fresh_when(:etag => @customer,
    :last_modified => @customer,
    :public => true)
    expires_in 10.minutes, :public => true
    Wednesday, October 24, 12

    View full-size slide

  87. Etag/Last Modified on the Server
    def show
    @customer = Customer.find(params[:id])
    fresh_when(:etag => @customer,
    :last_modified => @customer,
    :public => true)
    expires_in 10.minutes, :public => true
    end
    Wednesday, October 24, 12

    View full-size slide

  88. Etag/Last Modified on the Server
    def show
    @customer = Customer.find(params[:id])
    fresh_when(:etag => @customer,
    :last_modified => @customer,
    :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 24, 12

    View full-size slide

  89. Cache-Control limitations
    Server
    Wednesday, October 24, 12

    View full-size slide

  90. Cache-Control limitations
    Server
    Wednesday, October 24, 12

    View full-size slide

  91. Cache-Control limitations
    Server
    Cache-Control: max-age: 600
    Wednesday, October 24, 12

    View full-size slide

  92. Cache-Control limitations
    Server
    Cache-Control: max-age: 600
    Wednesday, October 24, 12

    View full-size slide

  93. Cache-Control limitations
    Server
    Cache-Control: max-age: 600 Cache-Control: max-age: 600
    Wednesday, October 24, 12

    View full-size slide

  94. Reverse-Proxy Cache FTW
    Server
    Varnish / Squid / Nginx
    Wednesday, October 24, 12

    View full-size slide

  95. Reverse-Proxy Cache FTW
    Server
    Varnish / Squid / Nginx
    Wednesday, October 24, 12

    View full-size slide

  96. Reverse-Proxy Cache FTW
    Server
    Varnish / Squid / Nginx
    Wednesday, October 24, 12

    View full-size slide

  97. Reverse-Proxy Cache FTW
    Server
    Cache-Control: max-age: 600
    Varnish / Squid / Nginx
    Wednesday, October 24, 12

    View full-size slide

  98. Reverse-Proxy Cache FTW
    Server
    Cache-Control: max-age: 600
    Varnish / Squid / Nginx
    Cache-Control: max-age: 600
    Wednesday, October 24, 12

    View full-size slide

  99. Reverse-Proxy Cache FTW
    Server
    Cache-Control: max-age: 600
    Varnish / Squid / Nginx
    Cache-Control: max-age: 600
    Wednesday, October 24, 12

    View full-size slide

  100. 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 24, 12

    View full-size slide

  101. 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 24, 12

    View full-size slide

  102. .JLF %PO`UFWFSDBMMNFBHBJO
    Wednesday, October 24, 12

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  105. Reason about your data
    • List of US States
    • User’s Profile
    • Customer’s Order
    • Activity Timeline
    • Yesterday’s stats
    Wednesday, October 24, 12

    View full-size slide

  106. Tradeoff
    Fresh

    View full-size slide

  107. Tradeoff
    Fresh

    View full-size slide

  108. Caching w/ AFNetworking
    Leverages NSURLCache
    already
    Wednesday, October 24, 12

    View full-size slide

  109. Caching w/ AFNetworking
    Customize if needed
    NSURLCache *cache =
    [[NSURLCache alloc] initWithMemoryCapacity:1024*1024*10
    diskCapacity:1024*1024*50
    diskPath:nil];
    [NSURLCache setSharedURLCache:cache];
    Wednesday, October 24, 12

    View full-size slide

  110. Consider alternatives
    SDURLCache
    http://github.com/steipete/SDURLCache
    Wednesday, October 24, 12

    View full-size slide

  111. Default Cache Location
    ~/Library/Caches/com.acme.app/Cache.db
    Wednesday, October 24, 12

    View full-size slide

  112. Wednesday, October 24, 12

    View full-size slide

  113. Switcheroo
    View Controller API Client
    Coordinator
    Wednesday, October 24, 12

    View full-size slide

  114. Switcheroo
    View Controller API Client
    Coordinator
    get data
    Wednesday, October 24, 12

    View full-size slide

  115. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    Wednesday, October 24, 12

    View full-size slide

  116. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    Wednesday, October 24, 12

    View full-size slide

  117. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    Wednesday, October 24, 12

    View full-size slide

  118. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    Wednesday, October 24, 12

    View full-size slide

  119. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    200 OK
    Wednesday, October 24, 12

    View full-size slide

  120. Switcheroo
    View Controller API Client
    Coordinator
    get data
    start request
    (cached data)
    HTTP GET
    200 OK
    (parsed response)
    Wednesday, October 24, 12

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  123. API Design Tips
    Don’t expose your internal model
    http://www.myspace.com/na6_build/photos/20613762
    Wednesday, October 24, 12

    View full-size slide

  124. API Design Tips
    Version your API
    Wednesday, October 24, 12

    View full-size slide

  125. API Design Tips
    Send OS Version, App Version,
    and Device Name as headers
    Device Name
    OS Version
    App Version
    Wednesday, October 24, 12

    View full-size slide

  126. API Design Tips
    Have a way to notify clients
    to upgrade (even force)
    Wednesday, October 24, 12

    View full-size slide

  127. 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 24, 12

    View full-size slide

  128. API Design Tips
    Page Large Datasets
    Wednesday, October 24, 12

    View full-size slide

  129. API Design Tips
    Build-in HTTP Caching support
    from day 1
    Wednesday, October 24, 12

    View full-size slide

  130. API Design Tips
    Take care when caching
    user-specific data
    Wednesday, October 24, 12

    View full-size slide

  131. API Design Tips
    Turn on Gzip Compression
    Wednesday, October 24, 12

    View full-size slide

  132. API Design Tips
    Measure your API response
    times
    Wednesday, October 24, 12

    View full-size slide

  133. Thank you!
    Ben Scheirman
    @subdigital
    nsscreencast.com
    Wednesday, October 24, 12

    View full-size slide