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 Slide

  2. Ben Scheirman
    CFOTDIFJSNBODPN !TVCEJHJUBM
    • Director

    View Slide

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

    View Slide

  4. Why is this important?
    99%

    View Slide

  5. Why is this important?
    Doing

    View Slide

  6. Why is this important?
    Everyone

    View Slide

  7. Why is this important?
    Everyone

    View Slide

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

    View Slide

  9. Exhibit A: Stuttery Scroller
    Accessing

    View Slide

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

    View Slide

  11. Exhibit B: The Perpetual Loader
    How

    View Slide

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

    View Slide

  13. Exhibit C: Slow Loader
    Loading

    View Slide

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

    View Slide

  15. Exhibit D: Yesterday’s News
    Caching

    View 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 Slide

  17. AFNetworking
    Wednesday, October 24, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  23. Why NSOperation?
    •Queues

    View Slide

  24. Usage
    Wednesday, October 24, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 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 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 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 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 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 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 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 Slide

  37. Requesting JSON
    Wednesday, October 24, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 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 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 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 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 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 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 Slide

  49. Requesting Images
    Wednesday, October 24, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 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 Slide

  57. Requesting XML?
    Wednesday, October 24, 12

    View Slide

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

    View Slide

  59. Requesting plist?
    Wednesday, October 24, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  63. API Clients

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  71. 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 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 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 Slide

  74. 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 Slide

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

    View Slide

  76. 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 Slide

  77. 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 Slide

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

    View Slide

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

    View Slide

  80. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  85. 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 Slide

  86. 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 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
    Wednesday, October 24, 12

    View 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
    Wednesday, October 24, 12

    View Slide

  89. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide

  101. 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 Slide

  102. 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 Slide

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

    View Slide

  104. 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 Slide

  105. 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 Slide

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

    View Slide

  107. Tradeoff
    Fresh

    View Slide

  108. Tradeoff
    Fresh

    View Slide

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

    View Slide

  110. 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 Slide

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

    View Slide

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

    View Slide

  113. Wednesday, October 24, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  122. 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 Slide

  123. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  128. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide