How to use Completion Handler in Swift
To know when a network request has been successfully completed while retrieving data from the web, completion handlers must be created. When carrying out lengthy operations, it comes in helpful.
You must learn how to hit an API and parse json data before reading any further in this article.
Syntax
func fetchData(completion: @escaping () -> Void) {
completion()
}Although at first glance it appears complex, we have simplified the fetchData() function.
- parameter of completion is taking escaping closure
escapingkeyword means that closure will executes when function returns
Here is another example that completion takes String parameter.
func fetchData(completion: @escaping (String) -> Void {
completion(result)
}In another article, we learned how to fetch Flickr API data and decode it using JSONDecoder. We will now add a completion handler to the network request for the Flickr API.
func fetchData(completion: @escaping (FlickrImageInfo) -> Void {
let url = URL(string:"https://api.flickr.com/services/rest/?method=flickr.
photos.search&api_key=526ada5ee1ea2cf61ac6bd3d3d2f405e&tags=kitten&page=0&
format=json&nojsoncallback=1")!
let task = URLSession.shared.dataTask(with: url) {
(data, response, error) in
let jsonDecoder = JSONDecoder()
if let data = data,
let result = try? jsonDecoder.decode(FlickrImageInfo.self, from: data) {
completion(result)
}
}
task.resume()
}We will now call the function and print the outcome.
fetchData { (result) in
print(result)
}The main issue with this code is that it does not account for any failures, such as when there is no response or when JSON cannot be decoded.
So to handle this situation we have to address errors using Result enum. It has two cases success or failure.
Syntax
func fetchData(completion: @escaping (Result <FlickrImageInfo, Error>) -> Void {
……
}Go ahead and make changes to our previous code example.
func fetchData(completion: @escaping (Result<FlickrImageInfo,Error>) -> Void {
let url = URL(string:"https://api.flickr.com/services/rest/?method=flickr.
photos.search&api_key=526ada5ee1ea2cf61ac6bd3d3d2f405e&tags=kitten&page=0&
format=json&nojsoncallback=1")!
let task = URLSession.shared.dataTask(with: url) {
(data, response, error) in
let jsonDecoder = JSONDecoder()
if let data = data {
do {
let result = try jsonDecoder.decode(FlickrImageInfo.self, from: data)
completion(.success(result))
} catch {
completion(.failure(error))
}
}
}
task.resume()
}Last make changes while calling that function.
fetchData { (result) in
switch result {
case .success(let info):
print(info)
case .failure(let errror):
print(error)
}
}Conclusion
It takes some practice to understand these patterns, but once you do, it becomes simple.
To find newest tips and tricks about iOS development, Xcode and Swift please visit https://www.gurjit.co
Thanks!
Related articles:




