Skip to content

Instantly share code, notes, and snippets.

@chenr2
Last active August 29, 2015 14:25

Revisions

  1. chenr2 revised this gist Jul 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -114,7 +114,7 @@ task.resume()

    ## A better way to POST

    This does the same thing.
    This does the same thing, but better

    ```swift
    let params = [ "foo" : "bar" ]
  2. chenr2 revised this gist Jul 20, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -122,7 +122,8 @@ request(.POST, "http://httpbin.org/post", parameters: params, encoding: .JSON)
    .validate()
    .responseJSON { (_, _, data, error) in
    if error == nil {
    println("data:: \(data)")
    let json = JSON(data!)
    println("json:: \(json)")
    }
    }
    ```
  3. chenr2 revised this gist Jul 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -79,7 +79,7 @@ request(.GET, url)
    }
    ```

    `validate()` checks these for you. Just remember to do `if error != nil`
    Thanks to `validate()`, the `error` is triggered not only when there's a bad physical connection, but also when you get a bad response.

    ## What about POST?

  4. chenr2 revised this gist Jul 20, 2015. 1 changed file with 10 additions and 6 deletions.
    16 changes: 10 additions & 6 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -60,6 +60,7 @@ Pain points:
    * Lots of boilerplate just to setup a network request. Imagine if you have a dozen API endpoints.
    * `NSJSONSerialization` is kind of yucky. We still need to protect against `jsonError` bombing.
    * Check out the `if-let` pyramid of doom
    * `error` is triggered if there was a physical network problem, like bad wifi. You still need to check for status codes (`404`, `500`) and for malformed JSON.

    ## GET using AlamoFire and SwiftyJSON

    @@ -70,15 +71,16 @@ let url = "https://itunes.apple.com/us/rss/topfreeapplications/limit=10/json"
    request(.GET, url)
    .validate()
    .responseJSON { (_, _, data, error) in
    let json = JSON(data!)
    let label = json["feed"]["entry"][0]["im:name"]["label"].stringValue
    println("label:: \(label)")
    if error == nil {
    let json = JSON(data!)
    let label = json["feed"]["entry"][0]["im:name"]["label"].stringValue
    println("label:: \(label)")
    }
    }
    ```

    Note: Normally, `error` is triggered if there was a physical network problem, like bad wifi. You still need to check for status codes (`404`, `500`) and for malformed JSON.

    `validate()` checks these for you. Just remember to do `if error != nil`

    ## What about POST?

    This is what a vanilla POST looks like. Not only do you need to convert your dictionary to JSON, you also need to set `application/json` headers.
    @@ -119,7 +121,9 @@ let params = [ "foo" : "bar" ]
    request(.POST, "http://httpbin.org/post", parameters: params, encoding: .JSON)
    .validate()
    .responseJSON { (_, _, data, error) in
    println("data:: \(data)")
    if error == nil {
    println("data:: \(data)")
    }
    }
    ```

  5. chenr2 revised this gist Jul 20, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -76,6 +76,9 @@ request(.GET, url)
    }
    ```

    Note: Normally, `error` is triggered if there was a physical network problem, like bad wifi. You still need to check for status codes (`404`, `500`) and for malformed JSON.

    `validate()` checks these for you. Just remember to do `if error != nil`
    ## What about POST?

    This is what a vanilla POST looks like. Not only do you need to convert your dictionary to JSON, you also need to set `application/json` headers.
  6. chenr2 revised this gist Jul 20, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -69,7 +69,7 @@ Syntax is tighter and easier to read.
    let url = "https://itunes.apple.com/us/rss/topfreeapplications/limit=10/json"
    request(.GET, url)
    .validate()
    .responseJSON { (_, _, data, _) in
    .responseJSON { (_, _, data, error) in
    let json = JSON(data!)
    let label = json["feed"]["entry"][0]["im:name"]["label"].stringValue
    println("label:: \(label)")
    @@ -115,7 +115,7 @@ This does the same thing.
    let params = [ "foo" : "bar" ]
    request(.POST, "http://httpbin.org/post", parameters: params, encoding: .JSON)
    .validate()
    .responseJSON { (_, _, data, _) in
    .responseJSON { (_, _, data, error) in
    println("data:: \(data)")
    }
    ```
  7. chenr2 revised this gist Jul 20, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -114,6 +114,7 @@ This does the same thing.
    ```swift
    let params = [ "foo" : "bar" ]
    request(.POST, "http://httpbin.org/post", parameters: params, encoding: .JSON)
    .validate()
    .responseJSON { (_, _, data, _) in
    println("data:: \(data)")
    }
  8. chenr2 revised this gist Jul 20, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -68,6 +68,7 @@ Syntax is tighter and easier to read.
    ```swift
    let url = "https://itunes.apple.com/us/rss/topfreeapplications/limit=10/json"
    request(.GET, url)
    .validate()
    .responseJSON { (_, _, data, _) in
    let json = JSON(data!)
    let label = json["feed"]["entry"][0]["im:name"]["label"].stringValue
  9. chenr2 revised this gist Jul 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -143,7 +143,7 @@ While this works, you still have to address 3 problems:
    * Cache validation. You need to inspect the downloaded image and make sure you don't cache a corrupt image.
    * `UIImage` thread safety. Now that you're caching, multiple cells will call `UIImage(data: data)` simultaneously and crash intermittently.

    (Btw, `NSURLConnection` is deprecated. Use `NSURLSession` instead because it supports HTTP/2 automatically.)
    (A fourth problem with the code above: `NSURLConnection` is deprecated. Use `NSURLSession` instead because it supports HTTP/2 automatically.)

    ## A better way to download images

  10. chenr2 revised this gist Jul 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -143,7 +143,7 @@ While this works, you still have to address 3 problems:
    * Cache validation. You need to inspect the downloaded image and make sure you don't cache a corrupt image.
    * `UIImage` thread safety. Now that you're caching, multiple cells will call `UIImage(data: data)` simultaneously and crash intermittently.

    (Btw, `NSURLConnection` is deprecated. Use `NSURLSession` instead because it supports HTTP/2. CDNs would benefit from the SPDY features, so image downloads especially should avoid `NSURLConnection`)
    (Btw, `NSURLConnection` is deprecated. Use `NSURLSession` instead because it supports HTTP/2 automatically.)

    ## A better way to download images

  11. chenr2 revised this gist Jul 20, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -137,14 +137,14 @@ NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue) {
    }
    ```

    (Btw, `NSURLConnection` is deprecated. Use `NSURLSession` instead because it supports HTTP/2. CDNs would benefit from the SPDY features, so image downloads especially should avoid `NSURLConnection`)

    While this works, you still have to address 3 problems:

    * Image caching. You need to respect your customer's data usage.
    * Cache validation. You need to inspect the downloaded image and make sure you don't cache a corrupt image.
    * `UIImage` thread safety. Now that you're caching, multiple cells will call `UIImage(data: data)` simultaneously and crash intermittently.

    (Btw, `NSURLConnection` is deprecated. Use `NSURLSession` instead because it supports HTTP/2. CDNs would benefit from the SPDY features, so image downloads especially should avoid `NSURLConnection`)

    ## A better way to download images

    This is how to download an image using the Network Starter Kit. It also fixes the 3 problems mentioned above.
  12. chenr2 revised this gist Jul 20, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -137,6 +137,8 @@ NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue) {
    }
    ```

    (Btw, `NSURLConnection` is deprecated. Use `NSURLSession` instead because it supports HTTP/2. CDNs would benefit from the SPDY features, so image downloads especially should avoid `NSURLConnection`)

    While this works, you still have to address 3 problems:

    * Image caching. You need to respect your customer's data usage.
  13. chenr2 revised this gist Jul 19, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -166,7 +166,6 @@ But there's also this `.responseImage()` that gives you back an image on a silve

    Go over the router

    ```
    ## Multipart

    Go over the multipart
  14. chenr2 revised this gist Jul 19, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -166,6 +166,7 @@ But there's also this `.responseImage()` that gives you back an image on a silve

    Go over the router

    ```
    ## Multipart
    Go over the multipart
  15. chenr2 revised this gist Jul 19, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ This is the script for Tuesday's Columbia iOS meetup.

    ## Some basics

    Thing | Contains
    Object | Contains
    ----- | --------
    URL | http://www.google.com?q=foo
    Request | verb, timeout, url, headers, body
  16. chenr2 revised this gist Jul 19, 2015. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,9 @@ This is the script for Tuesday's Columbia iOS meetup.
    Thing | Contains
    ----- | --------
    URL | http://www.google.com?q=foo


    Request | verb, timeout, url, headers, body
    Task | resume, cancel
    Session | like a browser session

    ## GET Request

  17. chenr2 revised this gist Jul 19, 2015. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,9 @@ This is the script for Tuesday's Columbia iOS meetup.

    ## Some basics

    | Thing | Contains |
    |||-
    | URL | http://www.google.com?q=foo |
    Thing | Contains
    ----- | --------
    URL | http://www.google.com?q=foo



    @@ -163,3 +163,8 @@ But there's also this `.responseImage()` that gives you back an image on a silve

    ## What if you have a lot of API calls?

    Go over the router

    ## Multipart

    Go over the multipart
  18. chenr2 revised this gist Jul 19, 2015. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,14 @@ See [Network Starter Kit](http://www.thorntech.com/2015/07/4-essential-swift-net

    This is the script for Tuesday's Columbia iOS meetup.

    ## Some basics

    | Thing | Contains |
    |||-
    | URL | http://www.google.com?q=foo |



    ## GET Request

    Problem statement.
  19. chenr2 revised this gist Jul 18, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -109,7 +109,7 @@ request(.POST, "http://httpbin.org/post", parameters: params, encoding: .JSON)
    }
    ```

    ## How about downloading images
    ## How about downloading images?

    This is the normal way:

    @@ -152,3 +152,6 @@ You see AlamoFire at work.
    But there's also this `.responseImage()` that gives you back an image on a silver platter. This is thanks to an Image Response Serializer from a RW tutorial.

    (Note to self: when doing the demo on a blank project, don't forget to follow the integration steps. You'll need to at least include the 2 `extension`s at the bottom of the Router. And the caching lines in `AppDelegate`)

    ## What if you have a lot of API calls?

  20. chenr2 revised this gist Jul 18, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -147,4 +147,8 @@ request(.GET, "http://tsumtsumdisney.com/wp-content/uploads/2014/10/Snow-White-a
    }
    ```

    (Note: when doing the demo on a blank project, don't forget to follow the integration steps. You'll need to at least include the 2 `extension`s at the bottom of the Router. And the caching lines in `AppDelegate`)
    You see AlamoFire at work.

    But there's also this `.responseImage()` that gives you back an image on a silver platter. This is thanks to an Image Response Serializer from a RW tutorial.

    (Note to self: when doing the demo on a blank project, don't forget to follow the integration steps. You'll need to at least include the 2 `extension`s at the bottom of the Router. And the caching lines in `AppDelegate`)
  21. chenr2 revised this gist Jul 18, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -130,9 +130,9 @@ NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue) {

    While this works, you still have to address 3 problems:

    # Image caching. You need to respect your customer's data usage.
    # Cache validation. You need to inspect the downloaded image and make sure you don't cache a corrupt image.
    # `UIImage` thread safety. Now that you're caching, multiple cells will call `UIImage(data: data)` simultaneously and crash intermittently.
    * Image caching. You need to respect your customer's data usage.
    * Cache validation. You need to inspect the downloaded image and make sure you don't cache a corrupt image.
    * `UIImage` thread safety. Now that you're caching, multiple cells will call `UIImage(data: data)` simultaneously and crash intermittently.

    ## A better way to download images

  22. chenr2 revised this gist Jul 18, 2015. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -128,11 +128,15 @@ NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue) {
    }
    ```

    ## A better way to download images
    While this works, you still have to address 3 problems:

    This is how to download an image using the Network Starter Kit.
    # Image caching. You need to respect your customer's data usage.
    # Cache validation. You need to inspect the downloaded image and make sure you don't cache a corrupt image.
    # `UIImage` thread safety. Now that you're caching, multiple cells will call `UIImage(data: data)` simultaneously and crash intermittently.

    (Note: when doing the demo on a blank project, don't forget to follow the integration steps. You'll need to at least include the 2 `extension`s at the bottom of the Router. And the caching lines in `AppDelegate`)
    ## A better way to download images

    This is how to download an image using the Network Starter Kit. It also fixes the 3 problems mentioned above.

    ```swift
    request(.GET, "http://tsumtsumdisney.com/wp-content/uploads/2014/10/Snow-White-and-the-Seven-Dwarfs-Tsum-Tsum-Set-of-8-0-0.jpg")
    @@ -143,3 +147,4 @@ request(.GET, "http://tsumtsumdisney.com/wp-content/uploads/2014/10/Snow-White-a
    }
    ```

    (Note: when doing the demo on a blank project, don't forget to follow the integration steps. You'll need to at least include the 2 `extension`s at the bottom of the Router. And the caching lines in `AppDelegate`)
  23. chenr2 revised this gist Jul 18, 2015. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -130,11 +130,16 @@ NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue) {

    ## A better way to download images

    This is how to download an image using the Network Starter Kit.

    (Note: when doing the demo on a blank project, don't forget to follow the integration steps. You'll need to at least include the 2 `extension`s at the bottom of the Router. And the caching lines in `AppDelegate`)

    ```swift
    request(.GET, "http://tsumtsumdisney.com/wp-content/uploads/2014/10/Snow-White-and-the-Seven-Dwarfs-Tsum-Tsum-Set-of-8-0-0.jpg")
    .responseImage() { (request, _, image, error) in
    if error == nil && image != nil {
    self.imageView.image = image
    }
    }
    ```
    ```

  24. chenr2 revised this gist Jul 18, 2015. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -126,4 +126,15 @@ NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue) {
    }
    }
    }
    ```

    ## A better way to download images

    ```swift
    request(.GET, "http://tsumtsumdisney.com/wp-content/uploads/2014/10/Snow-White-and-the-Seven-Dwarfs-Tsum-Tsum-Set-of-8-0-0.jpg")
    .responseImage() { (request, _, image, error) in
    if error == nil && image != nil {
    self.imageView.image = image
    }
    }
    ```
  25. chenr2 revised this gist Jul 18, 2015. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -107,4 +107,23 @@ request(.POST, "http://httpbin.org/post", parameters: params, encoding: .JSON)
    .responseJSON { (_, _, data, _) in
    println("data:: \(data)")
    }
    ```

    ## How about downloading images

    This is the normal way:

    ```swift
    let url = NSURL(string: "http://tsumtsumdisney.com/wp-content/uploads/2014/10/Snow-White-and-the-Seven-Dwarfs-Tsum-Tsum-Set-of-8-0-0.jpg")
    let request = NSURLRequest(URL: url!)
    let mainQueue = NSOperationQueue.mainQueue()
    NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue) {
    (response, data, error) -> Void in
    if error == nil {
    let image = UIImage(data: data)
    dispatch_async(dispatch_get_main_queue()) {
    self.imageView.image = image
    }
    }
    }
    ```
  26. chenr2 revised this gist Jul 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -52,7 +52,7 @@ Pain points:
    * `NSJSONSerialization` is kind of yucky. We still need to protect against `jsonError` bombing.
    * Check out the `if-let` pyramid of doom

    ## AlamoFire and SwiftyJSON to the rescue
    ## GET using AlamoFire and SwiftyJSON

    Syntax is tighter and easier to read.

  27. chenr2 revised this gist Jul 18, 2015. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,7 @@ This is the script for Tuesday's Columbia iOS meetup.

    Problem statement.

    Just to get this output:

    `label:: Agar.io`
    Just to get this output: `label:: Agar.io`

    we have all this code:

  28. chenr2 revised this gist Jul 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ This is the script for Tuesday's Columbia iOS meetup.

    ## GET Request

    This is what's wrong with the world
    Problem statement.

    Just to get this output:

  29. chenr2 revised this gist Jul 18, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,9 @@ See [Network Starter Kit](http://www.thorntech.com/2015/07/4-essential-swift-net

    This is the script for Tuesday's Columbia iOS meetup.

    ## GET Request: What's wrong with the world
    ## GET Request

    This is what's wrong with the world

    Just to get this output:

  30. chenr2 revised this gist Jul 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion demoNetwork.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ See [Network Starter Kit](http://www.thorntech.com/2015/07/4-essential-swift-net

    This is the script for Tuesday's Columbia iOS meetup.

    ## What's wrong with the world
    ## GET Request: What's wrong with the world

    Just to get this output: