Share
The Great iOS Concurrency Bake-Off - Swift Usergroup Netherlands, Amsterdam, February 2019
An Instant Replay™ of my concurrency talk from mobOS!
Abstract:
Callback Hell is a problem affecting developers of all stripes trying to write asynchronous code that's easy to follow. iOS developers don't yet have much in the way of built-in options to reduce callback hell, but there are several libraries which purport to help. In this session, you'll get a look at what problems are being solved by RxSwift and PromiseKit, as well as a look at an intriguing functional technique to avoid callback hell without libraries. And you'll learn more about how to answer the question: Which of these hammers is the most appropriate for my particular nail?
I will also make a ton of references to the Great British Bake Off, which you should watch because it is delightful.
Code:
https://github.com/designatednerd/OperationComparison
Original slides (they're pretty damned similar):
https://speakerdeck.com/designatednerd/the-great-ios-concurrency-bake-off-mobos-cluj-napoca-romania-february-2019
Ellen Shapiro PRO
Transcript
229 slides
-
The Great iOS Concurrency ! " Bake-Off ! Swift Usergroup
NL ! | Amsterdam | February 2019 @DesignatedNerd | bakkenbaeck.no | justhum.com -
Concurrency: "One thing should do these tasks at as close
to the same time as possible." -
Taking the result of an asynchronous function Giving it to
another asynchronous function -
Taking the result of an asynchronous function Giving it to
another asynchronous function (and another and another) -
As a user, I want to see a profile picture
when viewing a profile. -
As a developer, I want a list of profiles with
icons to not drop frames when scrolling. -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser(failureCompletion: @escaping (Error) -> Void, successCompletion: @escaping (User) -> Void) static func fetchImage(for user: User, failureCompletion: @escaping (Swift.Error) -> Void, successCompletion: @escaping (UIImage) -> Void) static func resizeImage(_ image: UIImage, to size: CGSize, failureCompletion: @escaping (Swift.Error) -> Void, successCompletion: @escaping (UIImage) -> Void) -
Not Horrible... self.activityIndicator.startAnimating() FakeAPI.fetchUser(failureCompletion: { [weak self] error in guard
let self = self else { return } self.activityIndicator.stopAnimating() self.operationLabel.text = "Error fetching user: \(error)" }, successCompletion: { [weak self] user in guard let self = self else { return } self.operationLabel.text = "Fetching image..." }) -
Not ideal... self.activityIndicator.startAnimating() FakeAPI.fetchUser(failureCompletion: { [weak self] error in guard
let self = self else { return } self.activityIndicator.stopAnimating() self.operationLabel.text = "Error fetching user: \(error)" }, successCompletion: { [weak self] user in guard let self = self else { return } self.operationLabel.text = "Fetching image..." RealAPI.fetchImage(for: user, failureCompletion: { [weak self] error in guard let self = self else { return } self.activityIndicator.stopAnimating() self.operationLabel.text = "Error fetching image: \(error)" }, successCompletion: { [weak self] image in guard let self = self else { return } self.operationLabel.text = "Resizing image..." }) }) -
...Does anyone have a ? self.activityIndicator.startAnimating() FakeAPI.fetchUser(failureCompletion: { [weak self]
error in guard let self = self else { return } self.activityIndicator.stopAnimating() self.operationLabel.text = "Error fetching user: \(error)" }, successCompletion: { [weak self] user in guard let self = self else { return } self.operationLabel.text = "Fetching image..." RealAPI.fetchImage(for: user, failureCompletion: { [weak self] error in guard let self = self else { return } self.activityIndicator.stopAnimating() self.operationLabel.text = "Error fetching image: \(error)" }, successCompletion: { [weak self] image in guard let self = self else { return } self.operationLabel.text = "Resizing image..." ImageResizer.resizeImage(image, to: self.imageView.frame.size, failureCompletion: { [weak self] error in guard let self = self else { return } self.activityIndicator.stopAnimating() self.operationLabel.text = "Error resizing image: \(error)" }, successCompletion: { [weak self] resizedImage in guard let self = self else { return } self.activityIndicator.stopAnimating() self.imageView.image = resizedImage self.operationLabel.text = "Complete in \(self.formattedSecondsSince(start))!" }) }) }) -
¯\_(ϑ)_/¯ Avoid UI interaction and don't care about retain cycles!*
* - not recommended -
FakeAPI.fetchUser(failureCompletion: { error in debugPrint("Error fetching user: \(error)") }, successCompletion:
{ user in RealAPI.fetchImage(for: user, failureCompletion: { error in debugPrint("Error fetching image: \(error)") }, successCompletion: { image in ImageResizer.resizeImage(image, to: self.imageView.frame.size, failureCompletion: { error in debugPrint("Error resizing image: \(error)") }, successCompletion: { resizedImage in self.imageView.image = resizedImage debugPrint("Complete in \(self.formattedSecondsSince(start))!" }) }) }) -
FakeAPI.fetchUser( errorCompletion: { error in debugPrint("Error fetching user: \(error)") },
successCompletion: { user in RealAPI.fetchImage( for: user, failureCompletion: { error in debugPrint("Error fetching image: \(error)") }, successCompletion: { image in ImageResizer.resizeImage( image, to: self.imageView.frame.size, failureCompletion: { error in debugPrint("Error resizing image: \(error)") }, successCompletion: { resizedImage in self.imageView.image = resizedImage debugPrint("Complete in \(self.formattedSecondsSince(start))!" }) }) }) -
private func fetchUser() { FakeAPI.fetchUser( failureCompletion: { error in debugPrint("Error
fetching user: \(error)") }, successCompletion: { user in self.fetchImage(for: user) }) } private func fetchImage(for user: User) { RealAPI.fetchImage( for: user, failureCompletion: { error in debugPrint("Error fetching image: \(error)") }, successCompletion: { image in self.resizeImage(image, to: self.imageView.frame.size) }) } private func resizeImage(_ image: UIImage, to size: CGSize) { ImageResizer.resizeImage( image, to: self.imageView.frame.size, failureCompletion: { error in debugPrint("Error resizing image: \(error)") }, successCompletion: { [weak self] resizedImage in guard let self = self else { return } self.imageView.image = resizedImage debugPrint("Complete in \(self.formattedSecondsSince(start))!") }) } -
private func fetchUser() { FakeAPI.fetchUser( failureCompletion: { error in debugPrint("Error
fetching user: \(error)") }, successCompletion: { user in self.fetchImage(for: user) }) } private func fetchImage(for user: User) { RealAPI.fetchImage( for: user, failureCompletion: { error in debugPrint("Error fetching image: \(error)") }, successCompletion: { image in self.resizeImage(image, to: self.imageView.frame.size) }) } private func resizeImage(_ image: UIImage, to size: CGSize) { ImageResizer.resizeImage( image, to: self.imageView.frame.size, failureCompletion: { error in debugPrint("Error resizing image: \(error)") }, successCompletion: { [weak self] resizedImage in guard let self = self else { return } self.imageView.image = resizedImage debugPrint("Complete in \(self.formattedSecondsSince(start))!") }) } -
private func fetchUser() { FakeAPI.fetchUser( failureCompletion: { error in debugPrint("Error
fetching user: \(error)") }, successCompletion: { user in self.fetchImage(for: user) }) } private func fetchImage(for user: User) { RealAPI.fetchImage( for: user, failureCompletion: { error in debugPrint("Error fetching image: \(error)") }, successCompletion: { image in self.resizeImage(image, to: self.imageView.frame.size) }) } private func resizeImage(_ image: UIImage, to size: CGSize) { ImageResizer.resizeImage( image, to: self.imageView.frame.size, failureCompletion: { error in debugPrint("Error resizing image: \(error)") }, successCompletion: { [weak self] resizedImage in guard let self = self else { return } self.imageView.image = resizedImage debugPrint("Complete in \(self.formattedSecondsSince(start))!") }) } -
FakeAPI.fetchUser( errorCompletion: { error in debugPrint("Error fetching user: \(error)") },
successCompletion: { user in RealAPI.fetchImage( for: user, failureCompletion: { error in debugPrint("Error fetching image: \(error)") }, successCompletion: { image in ImageResizer.resizeImage( image, to: self.imageView.frame.size, failureCompletion: { error in debugPrint("Error resizing image: \(error)") }, successCompletion: { resizedImage in self.imageView.image = resizedImage debugPrint("Complete in \(self.formattedSecondsSince(start))!" }) }) }) -
Result.Swift public enum Result<Success, Failure: Error> { /// A success,
storing a `Success` value. case success(Success) /// A failure, storing a `Failure` value. case failure(Failure) } -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser(failureCompletion: @escaping (Error) -> Void, successCompletion: @escaping (User) -> Void) static func fetchImage(for user: User, failureCompletion: @escaping (Swift.Error) -> Void, successCompletion: @escaping (UIImage) -> Void) static func resizeImage(_ image: UIImage, to size: CGSize, failureCompletion: @escaping (Swift.Error) -> Void, successCompletion: @escaping (UIImage) -> Void) -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser(completion: @escaping (Result<User, Error>) -> Void) static func fetchImage(for user: User, completion: @escaping (Result<UIImage, Error>) -> Void) static func resizeImage(_ image: UIImage, to size: CGSize, failureCompletion: @escaping (Result<UIImage, Error>) -> Void) -
FakeAPI.fetchUser { userResult in switch userResult { case .success(let user):
RealAPI.fetchImage(for: user) { imageResult in switch imageResult { case .success(let image): ImageResizer.resizeImage(image, to: self.imageView.frame.size) { resizedImageResult in switch resizedImageResult { case .success(let resizedImage): self.imageView.image = resizedImage debugPrint("Complete") case .failure(let error): debugPrint("Error resizing image: \(error)") } } case .failure(let error): debugPrint("Error fetching image: \(error)") } } case .failure(let error): debugPrint("Error fetching user: \(error)") } } -
FakeAPI.fetchUser { userResult in switch userResult { case .success(let user):
RealAPI.fetchImage(for: user) { imageResult in switch imageResult { case .success(let image): ImageResizer.resizeImage(image, to: self.imageView.frame.size) { resizedImageResult in switch resizedImageResult { case .success(let resizedImage): self.imageView.image = resizedImage debugPrint("Complete") case .failure(let error): debugPrint("Error resizing image: \(error)") } } case .failure(let error): debugPrint("Error fetching image: \(error)") } } case .failure(let error): debugPrint("Error fetching user: \(error)") } } -
FakeAPI.fetchUser { userResult in switch userResult { case .success(let user):
RealAPI.fetchImage(for: user) { imageResult in switch imageResult { case .success(let image): ImageResizer.resizeImage( image, to: self.imageView.frame.size) { resizedImageResult in switch resizedImageResult { case .success(let resizedImage): self.imageView.image = resizedImage debugPrint("Complete") case .failure(let error): debugPrint("Error resizing image: \(error)") } } case .failure(let error): debugPrint("Error fetching image: \(error)") } } case .failure(let error): debugPrint("Error fetching user: \(error)") } } -
FakeAPI.fetchUser { userResult in switch userResult { case .success(let user):
RealAPI.fetchImage(for: user) { imageResult in switch imageResult { case .success(let image): ImageResizer.resizeImage( image, to: self.imageView.frame.size) { resizedImageResult in switch resizedImageResult { case .success(let resizedImage): self.imageView.image = resizedImage debugPrint("Complete") case .failure(let error): debugPrint("Error resizing image: \(error)") } } case .failure(let error): debugPrint("Error fetching image: \(error)") } } case .failure(let error): debugPrint("Error fetching user: \(error)") } } -
How do we chain operations which need each others' results
in an easy-to-follow way? -
How do we chain operations which need each others' results
so we can figure out where something's going wrong? -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser(completion: @escaping (Result<User, Error>) -> Void) static func fetchImage(for user: User, completion: @escaping (Result<UIImage, Error>) -> Void) static func resizeImage(_ image: UIImage, to size: CGSize, failureCompletion: @escaping (Result<UIImage, Error>) -> Void) -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser(completion: @escaping (Result<User, Error>) -> Void) static func fetchImage(for user: User, completion: @escaping (Result<UIImage, Error>) -> Void) static func resizeImage(_ image: UIImage, to size: CGSize, failureCompletion: @escaping (Result<UIImage, Error>) -> Void) -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser(completion: @escaping (Result<User, Error>) -> Void) static func fetchImage(for user: User, completion: @escaping (Result<UIImage, Error>) -> Void) static func resizeImage(_ image: UIImage, to size: CGSize, failureCompletion: @escaping (Result<UIImage, Error>) -> Void) -
! Wrapper Methods private func resizeImageForImageView(_ image: UIImage, completion: @escaping
(Swift.Result<UIImage, Swift.Error>) -> Void) { ImageResizer.resizeImage(image, to: self.imageView.frame.size, completion: completion) } -
! Wrapper Methods private func resizeImageForImageView(_ image: UIImage, completion: @escaping
(Swift.Result<UIImage, Swift.Error>) -> Void) { self.operationLabel.text = "Resizing image (Custom Operator)..." ImageResizer.resizeImage(image, to: self.imageView.frame.size, completion: completion) } private func fetchUserImage(for user: User, completion: @escaping (Swift.Result<UIImage, Swift.Error>) -> Void) { self.operationLabel.text = "Fetching image (Custom Operator)..." RealAPI.fetchImage(for: user, completion: completion) } -
self.activityIndicator.startAnimating() let chain = API.fetchUser --> fetchUserImage --> resizeImageForImageView chain
{ result in self.activityIndicator.stopAnimating() switch result { case .success(let image): self.imageView.image = image self.operationLabel.text = "Complete (Custom Operator)!" case .error(let error): self.operationLabel.text = "Error occurred (Custom Operator): \(error)" } } -
self.activityIndicator.startAnimating() let chain = API.fetchUser --> fetchUserImage --> resizeImageForImageView chain
{ result in self.activityIndicator.stopAnimating() switch result { case .success(let image): self.imageView.image = image self.operationLabel.text = "Complete (Custom Operator)!" case .error(let error): self.operationLabel.text = "Error occurred (Custom Operator): \(error)" } } -
self.activityIndicator.startAnimating() let chain = API.fetchUser --> fetchUserImage --> resizeImageForImageView chain
{ result in self.activityIndicator.stopAnimating() switch result { case .success(let image): self.imageView.image = image self.operationLabel.text = "Complete (Custom Operator)!" case .error(let error): self.operationLabel.text = "Error occurred (Custom Operator): \(error)" } } -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
typealias ResultCompletion<T> = (Result<T, Error>) -> Void infix operator -->:
MultiplicationPrecedence func --><T,U>(_ firstFunction: @escaping (@escaping ResultCompletion<T>) -> Void, _ secondFunction: @escaping (T, @escaping ResultCompletion<U>) -> Void) -> (@escaping ResultCompletion<U>) -> Void { return { completion in firstFunction { result in switch result { case .success(let item): secondFunction(item) { result2 in completion(result2) } case .error(let error): completion(.error(error)) } } } } -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser(completion: @escaping (Result<User, Error>) -> Void) static func fetchImage(for user: User, completion: @escaping (Result<UIImage, Error>) -> Void) static func resizeImage(_ image: UIImage, to size: CGSize, failureCompletion: @escaping (Result<UIImage, Error>) -> Void) -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser() -> Promise<User> static func fetchImage(for user: User) -> Promise<Image> static func resizeImage(_ image: UIImage, to size: CGSize) -> Promise<Image> -
self.activityIndicator.startAnimating() FakeAPI.fetchUser() .then { user in RealAPI.fetchImage(for: user) } .then
{ image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
self.activityIndicator.startAnimating() FakeAPI.fetchUser() .then { user in RealAPI.fetchImage(for: user) } .then
{ image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
self.activityIndicator.startAnimating() FakeAPI.fetchUser() .then { user in RealAPI.fetchImage(for: user) } .then
{ image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
self.activityIndicator.startAnimating() FakeAPI.fetchUser() .then { user in RealAPI.fetchImage(for: user) } .then
{ image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
self.activityIndicator.startAnimating() FakeAPI.fetchUser() .then { user in RealAPI.fetchImage(for: user) } .then
{ image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
self.activityIndicator.startAnimating() FakeAPI.fetchUser() .then { user in RealAPI.fetchImage(for: user) } .then
{ image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
self.activityIndicator.startAnimating() FakeAPI.fetchUser() .then { user in RealAPI.fetchImage(for: user) } .then
{ image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser() -> Promise<User> static func fetchImage(for user: User) -> Promise<Image> static func resizeImage(_ image: UIImage, to size: CGSize) -> Promise<Image> -
User Data -> Icon Image -> Resized Icon Image static
func fetchUser() -> Single<User> static func fetchImage(for user: User) -> Single<Image> static func resizeImage(_ image: UIImage, to size: CGSize) -> Single<Image> -
Observable "I could get many values from this, until it
tells me things are complete or an error has occurred." -
FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap {
image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap {
image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap {
image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap {
image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap {
image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap {
image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
Operator Overloading private func fetchUserImage(for user: User, completion: @escaping (Result<UIImage>)
-> Void) { self.operationLabel.text = "Fetching image (Custom Operator)..." API.fetchImage(for: user, completion: completion) } -
Operator Overloading private func fetchUserImage(for user: User, completion: @escaping (Result<UIImage>)
-> Void) { self.operationLabel.text = "Fetching image (Custom Operator)..." DispatchQueue.global(qos: .background).async { API.fetchImage(for: user, completion: completion) } } -
Operator Overloading private func fetchUserImage(for user: User, completion: @escaping (Result<UIImage>)
-> Void) { self.operationLabel.text = "Fetching image (Custom Operator)..." DispatchQueue.global(qos: .background).async { API.fetchImage(for: user, completion: completion) } } -
Operator Overloading private func fetchUserImage(for user: User, completion: @escaping (Result<UIImage>)
-> Void) { self.operationLabel.text = "Fetching image (Custom Operator)..." DispatchQueue.global(qos: .background).async { API.fetchImage(for: user, completion: completion) } } -
PromiseKit FakeAPI.fetchUser() .then { user in RealAPI.fetchImage(for: user) } .then
{ image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
PromiseKit FakeAPI.fetchUser() .then(on: .global(qos: .background)) { user in RealAPI.fetchImage(for: user)
} .then(on: .global(qos: .background)) { image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
PromiseKit FakeAPI.fetchUser() .then(on: .global(qos: .background)) { user in RealAPI.fetchImage(for: user)
} .then(on: .global(qos: .background)) { image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
PromiseKit FakeAPI.fetchUser() .then(on: .global(qos: .background)) { user in RealAPI.fetchImage(for: user)
} .then(on: .global(qos: .background)) { image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
PromiseKit FakeAPI.fetchUser() .then(on: .global(qos: .background)) { user in RealAPI.fetchImage(for: user)
} .map { image in (image, self.imageView.frame.size) } .then(on: .global(qos: .background)) { tuple in ImageResizer.resizeImage(tuple.0, to: tuple.1) } .then(on: .global(qos: .background)) { image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
PromiseKit FakeAPI.fetchUser() .then(on: .global(qos: .background)) { user in RealAPI.fetchImage(for: user)
} .map { image in (image, self.imageView.frame.size) } .then(on: .global(qos: .background)) { tuple in ImageResizer.resizeImage(tuple.0, to: tuple.1) } .then(on: .global(qos: .background)) { image in ImageResizer.resizeImage(image, to: self.imageView.frame.size) } .ensure { self.activityIndicator.stopAnimating() } .done { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (PromiseKit)!" } .catch { [weak self] error in self?.operationLabel.text = "Error occurred (PromiseKit): \(error)" } -
It's not a queue! (it's just way easier to think
of it as one) -
RxSwift FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap
{ image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
RxSwift FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap
{ image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .observeOn(MainScheduler.instance) .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
RxSwift FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap
{ image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .observeOn(MainScheduler.instance) .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
RxSwift FakeAPI.rx.fetchUser() .flatMap { user in RealAPI.rx.fetchImage(for: user) } .flatMap
{ image in ImageResizer.rx.resizeImage(image, to: self.imageView.frame.size) } .subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background)) .observeOn(MainScheduler.instance) .subscribe( onSuccess: { resizedImage in self.imageView.image = resizedImage self.operationLabel.text = "Complete (RxSwift) in \(self.formattedSecondsSince(start))!" }, onError: { error in self.operationLabel.text = "Error occurred (RxSwift): \(error)" } ) .disposed(by: bag) -
PromiseKit: Perform on a queue you specify Call back on
a pre-defined queue -
PromiseKit: Same Type let userPromise1 = FakeAPI.fetchUser() let userPromise2 =
FakeAPI.fetchUser() when(fulfilled: [userPromise1, userPromise2]) .done { users in debugPrint("Users: \(users)") } .catch { error in debugPrint("Error: \(error)") } -
PromiseKit: Same Type let userPromise1 = FakeAPI.fetchUser() let userPromise2 =
FakeAPI.fetchUser() when(fulfilled: [userPromise1, userPromise2]) .done { users in debugPrint("Users: \(users)") } .catch { error in debugPrint("Error: \(error)") } -
PromiseKit: Same Type let userPromise1 = FakeAPI.fetchUser() let userPromise2 =
FakeAPI.fetchUser() when(fulfilled: [userPromise1, userPromise2]) .done { users in debugPrint("Users: \(users)") } .catch { error in debugPrint("Error: \(error)") } -
PromiseKit: Same Type let userPromise1 = FakeAPI.fetchUser() let userPromise2 =
FakeAPI.fetchUser() when(fulfilled: userPromise1, userPromise2) .done { user1, user2 in debugPrint("User1: \(user1), User2: \(user2)") } .catch { error in debugPrint("Error: \(error)") } -
PromiseKit: Same Type let userPromise1 = FakeAPI.fetchUser() let userPromise2 =
FakeAPI.fetchUser() when(fulfilled: userPromise1, userPromise2) .done { user1, user2 in debugPrint("User1: \(user1), User2: \(user2)") } .catch { error in debugPrint("Error: \(error)") } -
PromiseKit: Different Types let userPromise1 = FakeAPI.fetchUser() let imagePromise =
ImageResizer.resizeImage(UIImage(), to: self.imageView.frame.size) when(fulfilled: userPromise1, imagePromise) .done { user, image in debugPrint("User: \(user), image: \(image)") } .catch { error in debugPrint("Error: \(error)") } -
PromiseKit: Different Types let userPromise1 = FakeAPI.fetchUser() let imagePromise =
ImageResizer.resizeImage(UIImage(), to: self.imageView.frame.size) when(fulfilled: userPromise1, imagePromise) .done { user, image in debugPrint("User: \(user), image: \(image)") } .catch { error in debugPrint("Error: \(error)") } -
PromiseKit: Different Types let userPromise1 = FakeAPI.fetchUser() let imagePromise =
ImageResizer.resizeImage(UIImage(), to: self.imageView.frame.size) when(fulfilled: userPromise1, imagePromise) .done { user, image in debugPrint("User: \(user), image: \(image)") } .catch { error in debugPrint("Error: \(error)") } -
Obligatory Summary Slide → Concurrency is harder than it looks.
→ You can try to handle it yourself, but it's going to be a difficult time sink. -
Obligatory Summary Slide → Concurrency is harder than it looks.
→ You can try to handle it yourself, but it's going to be a difficult time sink. → PromiseKit is great for most concurrency handling and readability -
Obligatory Summary Slide → Concurrency is harder than it looks.
→ You can try to handle it yourself, but it's going to be a difficult time sink. → PromiseKit is great for most concurrency handling and readability → RxSwift is a giant hammer...but sometimes you need a giant hammer. -
Links! → Vincent Pradeilles' Function Composition Talk https://vimeo.com/292702159 → John
Sundell's Tasks post https://www.swiftbysundell.com/posts/task-based- concurrency-in-swift → RxMarbles Visualization https://rxmarbles.com -
! The Future! → Chris Lattner's Concurrency Manifesto https://gist.github.com/lattner/ 31ed37682ef1576b16bca1432ea9f782
→ Kotlin Coroutines: Async/Await + More https://kotlinlang.org/docs/reference/coroutines- overview.html