Last active
August 29, 2015 14:25
Revisions
-
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, but better ```swift let params = [ "foo" : "bar" ] -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 { let json = JSON(data!) println("json:: \(json)") } } ``` -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -79,7 +79,7 @@ request(.GET, url) } ``` 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? -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 10 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 if error == nil { let json = JSON(data!) let label = json["feed"]["entry"][0]["im:name"]["label"].stringValue println("label:: \(label)") } } ``` `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 if error == nil { println("data:: \(data)") } } ``` -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, 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, error) in println("data:: \(data)") } ``` -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)") } -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. (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 -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 better way to download images -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -137,14 +137,14 @@ 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. (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. -
chenr2 revised this gist
Jul 20, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
chenr2 revised this gist
Jul 19, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
chenr2 revised this gist
Jul 19, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
chenr2 revised this gist
Jul 19, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 Object | Contains ----- | -------- URL | http://www.google.com?q=foo Request | verb, timeout, url, headers, body -
chenr2 revised this gist
Jul 19, 2015 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
chenr2 revised this gist
Jul 19, 2015 . 1 changed file with 8 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 @@ -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 -
chenr2 revised this gist
Jul 19, 2015 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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? 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? -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } ``` 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`) -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. ## A better way to download images -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 8 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -128,11 +128,15 @@ 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. ## 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`) -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } } ``` -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } } ``` -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 19 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } } } ``` -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ## GET using AlamoFire and SwiftyJSON Syntax is tighter and easier to read. -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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` we have all this code: -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 Problem statement. Just to get this output: -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This is what's wrong with the world Just to get this output: -
chenr2 revised this gist
Jul 18, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. ## GET Request: What's wrong with the world Just to get this output:
NewerOlder