SlideShare a Scribd company logo
1 of 11
Download to read offline
Extending Foundation
Swift Meetup Hamburg • 2016–09–21 • @herzi
Recap: Promises
createMagicPromise().then { reply in
if reply.response.statusCode != 200 {
throw RESTError.badResponse(reply.response)
}
// 🤔 : Still need to validate the content type.
return JSON.parse(data: reply.data)
}.then { json in
return User(serialized: json)
}.then { user in
// Do something with the user object.
}.fail { error in
// Don’t handle errors like this when being outside of keynote.
fatalError("FIXME: Handle error: (error)")
}.done()
URLSession + Promises
typealias Reply = (response: URLResponse,
data: Data)
// I really don’t have a better name.
protocol RESTful {
func get(url: URL) -> Promise<Reply>
}
URLSession
+Promise.swift
URLSession+Promise
extension URLSession: RESTful {
func get(url: URL) -> Promise<Reply> {
/* TODO: Use
* URLSession.dataTask(with:, completionHandler:)
*/
fatalError("Unimplemented.")
}
}
// Looks good, no?
URLSession+Promise
extension URLSession: RESTful {
func get(url: URL) -> Promise<Reply> {
/* TODO: Use
* URLSession.dataTask(with:, completionHandler:)
*/
fatalError("Unimplemented.")
}
}
// Looks good, no? No.
How to Test?
extension URLSession: RESTful {
func get(url: URL) -> Promise<Reply> {
/* TODO: Use
* URLSession.dataTask(with:, completionHandler:)
*/
fatalError("Unimplemented.")
}
}
// Looks good, no? No, I don’t want to set up an HTTP server.
Let’s try again
protocol URLSessionLike {
typealias Completion = (Data?, URLResponse?, Error?) -> Void
func dataTask(with url: URL,
completionHandler: @escaping Completion)
-> URLSessionDataTask
}
extension URLSession: URLSessionLike {}
Let’s try again (cont.)
protocol URLSessionLike: RESTful {
typealias Completion = (Data?, URLResponse?,
Error?) -> Void
func dataTask(with url: URL,
completionHandler: @escaping Completion)
-> URLSessionDataTask
}
extension URLSession: URLSessionLike {}



extension URLSessionLike {
func get(url: URL) -> Promise<Reply> {
fatalError("Still unimplemented.")
}
}
Testing the Protocol
class MockSession: URLSessionLike {
internal func dataTask(with url: URL,
completionHandler: @escaping
URLSessionLike.Completion)
-> URLSessionDataTask
{
DispatchQueue.main.async {
let error = URLError(.notConnectedToInternet)
completionHandler(nil, nil, error)
}
}
}
/* Later you will only mock RESTful things and return
* Promises directly. */
Questions?
Slides will be uploaded and linked in the Meetup Group

More Related Content

What's hot

Put a little Backbone in your WordPress
Put a little Backbone in your WordPressPut a little Backbone in your WordPress
Put a little Backbone in your WordPressadamsilverstein
 
API Days Australia - Automatic Testing of (RESTful) API Documentation
API Days Australia  - Automatic Testing of (RESTful) API DocumentationAPI Days Australia  - Automatic Testing of (RESTful) API Documentation
API Days Australia - Automatic Testing of (RESTful) API DocumentationRouven Weßling
 
Node.js Lightning Talk
Node.js Lightning TalkNode.js Lightning Talk
Node.js Lightning TalkCodeSlice
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + djangoNina Zakharenko
 
JavaScript : A trending scripting language
JavaScript : A trending scripting languageJavaScript : A trending scripting language
JavaScript : A trending scripting languageAbhayDhupar
 
Gojko Adzic Cucumber
Gojko Adzic CucumberGojko Adzic Cucumber
Gojko Adzic CucumberSkills Matter
 
JSChannel 2017 - Service Workers and the Role they Play in modern day web-apps
JSChannel 2017 - Service Workers and the Role they Play in modern day web-appsJSChannel 2017 - Service Workers and the Role they Play in modern day web-apps
JSChannel 2017 - Service Workers and the Role they Play in modern day web-appsMukul Jain
 
I18n
I18nI18n
I18nsoon
 
Live Streaming & Server Sent Events
Live Streaming & Server Sent EventsLive Streaming & Server Sent Events
Live Streaming & Server Sent Eventstkramar
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajaxbaygross
 
Google App Engine with Gaelyk
Google App Engine with GaelykGoogle App Engine with Gaelyk
Google App Engine with GaelykChoong Ping Teo
 
React Under the Hood: Understanding React's UI Rendering Process
React Under the Hood: Understanding React's UI Rendering ProcessReact Under the Hood: Understanding React's UI Rendering Process
React Under the Hood: Understanding React's UI Rendering ProcessAnthony Garritano
 
Effective Testing using Behavior-Driven Development
Effective Testing using Behavior-Driven DevelopmentEffective Testing using Behavior-Driven Development
Effective Testing using Behavior-Driven DevelopmentAlexander Kress
 

What's hot (14)

Put a little Backbone in your WordPress
Put a little Backbone in your WordPressPut a little Backbone in your WordPress
Put a little Backbone in your WordPress
 
API Days Australia - Automatic Testing of (RESTful) API Documentation
API Days Australia  - Automatic Testing of (RESTful) API DocumentationAPI Days Australia  - Automatic Testing of (RESTful) API Documentation
API Days Australia - Automatic Testing of (RESTful) API Documentation
 
Node.js Lightning Talk
Node.js Lightning TalkNode.js Lightning Talk
Node.js Lightning Talk
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + django
 
JavaScript : A trending scripting language
JavaScript : A trending scripting languageJavaScript : A trending scripting language
JavaScript : A trending scripting language
 
Gojko Adzic Cucumber
Gojko Adzic CucumberGojko Adzic Cucumber
Gojko Adzic Cucumber
 
Ruby de Rails
Ruby de RailsRuby de Rails
Ruby de Rails
 
JSChannel 2017 - Service Workers and the Role they Play in modern day web-apps
JSChannel 2017 - Service Workers and the Role they Play in modern day web-appsJSChannel 2017 - Service Workers and the Role they Play in modern day web-apps
JSChannel 2017 - Service Workers and the Role they Play in modern day web-apps
 
I18n
I18nI18n
I18n
 
Live Streaming & Server Sent Events
Live Streaming & Server Sent EventsLive Streaming & Server Sent Events
Live Streaming & Server Sent Events
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
 
Google App Engine with Gaelyk
Google App Engine with GaelykGoogle App Engine with Gaelyk
Google App Engine with Gaelyk
 
React Under the Hood: Understanding React's UI Rendering Process
React Under the Hood: Understanding React's UI Rendering ProcessReact Under the Hood: Understanding React's UI Rendering Process
React Under the Hood: Understanding React's UI Rendering Process
 
Effective Testing using Behavior-Driven Development
Effective Testing using Behavior-Driven DevelopmentEffective Testing using Behavior-Driven Development
Effective Testing using Behavior-Driven Development
 

Viewers also liked

IRS Tax Tips for Year End Gifts to Charity
IRS Tax Tips for Year End Gifts to CharityIRS Tax Tips for Year End Gifts to Charity
IRS Tax Tips for Year End Gifts to CharityTax Assistance Group
 
พระไตรปิฎกฉบับหลวงเล่มที่๐๘
พระไตรปิฎกฉบับหลวงเล่มที่๐๘พระไตรปิฎกฉบับหลวงเล่มที่๐๘
พระไตรปิฎกฉบับหลวงเล่มที่๐๘Rose Banioki
 
Moose Hunting in Alaska BOW Presentation 2016
Moose Hunting in Alaska BOW Presentation 2016Moose Hunting in Alaska BOW Presentation 2016
Moose Hunting in Alaska BOW Presentation 2016Becky Schwanke
 
Catalogo corsi di formazione The Energy Audit 2014
Catalogo corsi di formazione The Energy Audit 2014Catalogo corsi di formazione The Energy Audit 2014
Catalogo corsi di formazione The Energy Audit 2014TheEnergyAudit
 
Transforamções de unidades
Transforamções de unidadesTransforamções de unidades
Transforamções de unidadesLuciana Oliveira
 
พระไตรปิฎกฉบับหลวงเล่มที่๐๙
พระไตรปิฎกฉบับหลวงเล่มที่๐๙พระไตรปิฎกฉบับหลวงเล่มที่๐๙
พระไตรปิฎกฉบับหลวงเล่มที่๐๙Rose Banioki
 

Viewers also liked (16)

IRS Tax Tips for Year End Gifts to Charity
IRS Tax Tips for Year End Gifts to CharityIRS Tax Tips for Year End Gifts to Charity
IRS Tax Tips for Year End Gifts to Charity
 
พระไตรปิฎกฉบับหลวงเล่มที่๐๘
พระไตรปิฎกฉบับหลวงเล่มที่๐๘พระไตรปิฎกฉบับหลวงเล่มที่๐๘
พระไตรปิฎกฉบับหลวงเล่มที่๐๘
 
1,SARAD Resume
1,SARAD Resume1,SARAD Resume
1,SARAD Resume
 
Team work jueves 5.00-7.00 pm
Team work  jueves 5.00-7.00 pmTeam work  jueves 5.00-7.00 pm
Team work jueves 5.00-7.00 pm
 
Moose Hunting in Alaska BOW Presentation 2016
Moose Hunting in Alaska BOW Presentation 2016Moose Hunting in Alaska BOW Presentation 2016
Moose Hunting in Alaska BOW Presentation 2016
 
Catalogo corsi di formazione The Energy Audit 2014
Catalogo corsi di formazione The Energy Audit 2014Catalogo corsi di formazione The Energy Audit 2014
Catalogo corsi di formazione The Energy Audit 2014
 
5 Tax Breaks For Entrepreneurs
5 Tax Breaks For Entrepreneurs5 Tax Breaks For Entrepreneurs
5 Tax Breaks For Entrepreneurs
 
014
014014
014
 
diapositiva 2
diapositiva 2diapositiva 2
diapositiva 2
 
Y
YY
Y
 
Transforamções de unidades
Transforamções de unidadesTransforamções de unidades
Transforamções de unidades
 
พระไตรปิฎกฉบับหลวงเล่มที่๐๙
พระไตรปิฎกฉบับหลวงเล่มที่๐๙พระไตรปิฎกฉบับหลวงเล่มที่๐๙
พระไตรปิฎกฉบับหลวงเล่มที่๐๙
 
Catalogo Navideño Colombina Guatemala 2015, en Comprabien Foodservice, PBX 24...
Catalogo Navideño Colombina Guatemala 2015, en Comprabien Foodservice, PBX 24...Catalogo Navideño Colombina Guatemala 2015, en Comprabien Foodservice, PBX 24...
Catalogo Navideño Colombina Guatemala 2015, en Comprabien Foodservice, PBX 24...
 
Hablemos Sin Tapujos.
Hablemos Sin Tapujos.Hablemos Sin Tapujos.
Hablemos Sin Tapujos.
 
Inclusive growth
Inclusive growthInclusive growth
Inclusive growth
 
Accomodation
AccomodationAccomodation
Accomodation
 

Similar to Swift Meetup HH 2016/09

async/await in Swift
async/await in Swiftasync/await in Swift
async/await in SwiftPeter Friese
 
Progressive Enhancment with Rails and React
Progressive Enhancment with Rails and ReactProgressive Enhancment with Rails and React
Progressive Enhancment with Rails and ReactTyler Johnston
 
TPSE Thailand 2015 - Rethinking Web with React and Flux
TPSE Thailand 2015 - Rethinking Web with React and FluxTPSE Thailand 2015 - Rethinking Web with React and Flux
TPSE Thailand 2015 - Rethinking Web with React and FluxJirat Kijlerdpornpailoj
 
Data models in Angular 1 & 2
Data models in Angular 1 & 2Data models in Angular 1 & 2
Data models in Angular 1 & 2Adam Klein
 
Koajs as an alternative to Express - OdessaJs'16
Koajs as an alternative to Express - OdessaJs'16Koajs as an alternative to Express - OdessaJs'16
Koajs as an alternative to Express - OdessaJs'16Nikolay Kozhukharenko
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineAndy McKay
 
Mobile Day - React Native
Mobile Day - React NativeMobile Day - React Native
Mobile Day - React NativeSoftware Guru
 
Building a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsBuilding a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsNicolas Hery
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Railsrstankov
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescriptDavid Furber
 
Advanced Javascript Unit Testing
Advanced Javascript Unit TestingAdvanced Javascript Unit Testing
Advanced Javascript Unit TestingLars Thorup
 
REST more with json-api and fractal
REST more with json-api and fractalREST more with json-api and fractal
REST more with json-api and fractalBoyan Yordanov
 
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!JSFestUA
 
Server Side Swift - AppBuilders 2017
Server Side Swift - AppBuilders 2017Server Side Swift - AppBuilders 2017
Server Side Swift - AppBuilders 2017Jens Ravens
 
The Exciting Future Of React
The Exciting Future Of ReactThe Exciting Future Of React
The Exciting Future Of Reactkristijanmkd
 
Ba Node.js Meetup React Native Presentation
Ba Node.js Meetup React Native PresentationBa Node.js Meetup React Native Presentation
Ba Node.js Meetup React Native PresentationGustavo Machado
 

Similar to Swift Meetup HH 2016/09 (20)

async/await in Swift
async/await in Swiftasync/await in Swift
async/await in Swift
 
Progressive Enhancment with Rails and React
Progressive Enhancment with Rails and ReactProgressive Enhancment with Rails and React
Progressive Enhancment with Rails and React
 
Itjsf320
Itjsf320Itjsf320
Itjsf320
 
huhu
huhuhuhu
huhu
 
TPSE Thailand 2015 - Rethinking Web with React and Flux
TPSE Thailand 2015 - Rethinking Web with React and FluxTPSE Thailand 2015 - Rethinking Web with React and Flux
TPSE Thailand 2015 - Rethinking Web with React and Flux
 
Data models in Angular 1 & 2
Data models in Angular 1 & 2Data models in Angular 1 & 2
Data models in Angular 1 & 2
 
Koajs as an alternative to Express - OdessaJs'16
Koajs as an alternative to Express - OdessaJs'16Koajs as an alternative to Express - OdessaJs'16
Koajs as an alternative to Express - OdessaJs'16
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App Engine
 
Mobile Day - React Native
Mobile Day - React NativeMobile Day - React Native
Mobile Day - React Native
 
Itjsf13
Itjsf13Itjsf13
Itjsf13
 
Building a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsBuilding a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.js
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
Advanced Javascript Unit Testing
Advanced Javascript Unit TestingAdvanced Javascript Unit Testing
Advanced Javascript Unit Testing
 
REST more with json-api and fractal
REST more with json-api and fractalREST more with json-api and fractal
REST more with json-api and fractal
 
Itjsf13
Itjsf13Itjsf13
Itjsf13
 
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
 
Server Side Swift - AppBuilders 2017
Server Side Swift - AppBuilders 2017Server Side Swift - AppBuilders 2017
Server Side Swift - AppBuilders 2017
 
The Exciting Future Of React
The Exciting Future Of ReactThe Exciting Future Of React
The Exciting Future Of React
 
Ba Node.js Meetup React Native Presentation
Ba Node.js Meetup React Native PresentationBa Node.js Meetup React Native Presentation
Ba Node.js Meetup React Native Presentation
 

Recently uploaded

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Recently uploaded (20)

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

Swift Meetup HH 2016/09

  • 1. Extending Foundation Swift Meetup Hamburg • 2016–09–21 • @herzi
  • 2. Recap: Promises createMagicPromise().then { reply in if reply.response.statusCode != 200 { throw RESTError.badResponse(reply.response) } // 🤔 : Still need to validate the content type. return JSON.parse(data: reply.data) }.then { json in return User(serialized: json) }.then { user in // Do something with the user object. }.fail { error in // Don’t handle errors like this when being outside of keynote. fatalError("FIXME: Handle error: (error)") }.done()
  • 3. URLSession + Promises typealias Reply = (response: URLResponse, data: Data) // I really don’t have a better name. protocol RESTful { func get(url: URL) -> Promise<Reply> }
  • 5. URLSession+Promise extension URLSession: RESTful { func get(url: URL) -> Promise<Reply> { /* TODO: Use * URLSession.dataTask(with:, completionHandler:) */ fatalError("Unimplemented.") } } // Looks good, no?
  • 6. URLSession+Promise extension URLSession: RESTful { func get(url: URL) -> Promise<Reply> { /* TODO: Use * URLSession.dataTask(with:, completionHandler:) */ fatalError("Unimplemented.") } } // Looks good, no? No.
  • 7. How to Test? extension URLSession: RESTful { func get(url: URL) -> Promise<Reply> { /* TODO: Use * URLSession.dataTask(with:, completionHandler:) */ fatalError("Unimplemented.") } } // Looks good, no? No, I don’t want to set up an HTTP server.
  • 8. Let’s try again protocol URLSessionLike { typealias Completion = (Data?, URLResponse?, Error?) -> Void func dataTask(with url: URL, completionHandler: @escaping Completion) -> URLSessionDataTask } extension URLSession: URLSessionLike {}
  • 9. Let’s try again (cont.) protocol URLSessionLike: RESTful { typealias Completion = (Data?, URLResponse?, Error?) -> Void func dataTask(with url: URL, completionHandler: @escaping Completion) -> URLSessionDataTask } extension URLSession: URLSessionLike {}
 
 extension URLSessionLike { func get(url: URL) -> Promise<Reply> { fatalError("Still unimplemented.") } }
  • 10. Testing the Protocol class MockSession: URLSessionLike { internal func dataTask(with url: URL, completionHandler: @escaping URLSessionLike.Completion) -> URLSessionDataTask { DispatchQueue.main.async { let error = URLError(.notConnectedToInternet) completionHandler(nil, nil, error) } } } /* Later you will only mock RESTful things and return * Promises directly. */
  • 11. Questions? Slides will be uploaded and linked in the Meetup Group