Async/await is a new language feature that will ship with Swift 5.5 this year. There’s no doubt it will have a significant impact on how we write asynchronous code.
In this talk, we’re going to look at some use cases for async/await, how we can call existing Swift APIs using this new feature, and why your decision to write your SDK in Objective-C might turn out to have been a very clever move.
We’ll also have a look at the refactoring support Apple is adding to Xcode and how it will help you migrate your existing code base.
Simplifying Microservices & Apps - The art of effortless development - Meetup...
async/await in Swift
1. async/await in Swi!
Photo by Stephen H on Unsplash
Peter Friese | Firebase Developer Advocate | @pete!riese
2. let req = URLRequest(url: URL(string: “https:!"yourapi/path“)!)
let task = URLSession.shared.dataTask(with: req) { data, response, error in
!" do stuff once data arrives
}
task.resume()
Asynchronous code is everywhere
completion handler
3. func process(url: String, completion: @escaping (Article) !# Void) {
self.fetchArticle(from: url) { html in
self.extractTitle(from: html) { title in
self.extractText(from: html) { text in
self.extractImage(from: url) { imageUrl in
self.inferTags(from: text) { tags in
let article = Article(url: url,
title: title,
tags: tags,
imageUrlString: imageUrl)
completion(article)
}
}
}
}
}
}
A more complex example
4. extension ArticleAnalyser {
func process(url: String, completion: @escaping (Article) !# Void) {
self.fetchArticle(from: url) { result in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let html):
self.extractTitle(from: html) { result in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let title):
self.extractText(from: html) { result in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let text):
Let’s add some error handling…
!
5. extension AsyncArticleAnalyser {
func process(url: String) async throws !# Article {
let htmlText = try await fetchArticle(from: url)
let text = try await extractText(from: htmlText)
let title = try await extractTitle(from: htmlText)
let imageUrl = try await extractImage(from: url)
let tags = await inferTags(from: text)
return Article(url: url,
title: title,
tags: tags,
imageUrlString: imageUrl)
}
}
How about this?
Swift 5.5
24. auth!$signInAnonymously()
let user = auth!$currentUser
print("User signed in with user ID: (user!$uid)")
auth!$signInAnonymously { result, error in
guard let result = result else {
return
}
print("User signed in with user ID: (result.user.uid)")
}
Use callbacks instead
25. auth!$signInAnonymously()
let user = auth!$currentUser
print("User signed in with user ID: (user!$uid)")
auth!$signInAnonymously { result, error in
guard let result = result else {
return
}
print("User signed in with user ID: (result.user.uid)")
}
@Published var user: User?
!!%
auth!$signInAnonymously()
.map { $0.user }
.replaceError(with: nil)
.assign(to: &$user)
Combine
26. auth!$signInAnonymously { result, error in
guard let result = result else {
return
}
print("User signed in with user ID: (result.user.uid)")
}
Firebase and async/await
27. auth!$signInAnonymously { result, error in
guard let result = result else {
return
}
print("User signed in with user ID: (result.user.uid)")
}
Firebase and async/await
do {
let result = try await auth!$signInAnonymously()
print("User signed in with user ID: (result.user.uid)")
}
catch {
print(error)
}
Callback-style
Async/await
28. Here is how much work it was
to implement for Firebase:
.
(yep - it came almost for free)