How to use Completion Handler in Swift

JSON Networking

26 Aug 2023 . 2 min read       @gurjitpt


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.


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 {
    ……
}


Make necessary changes to the previous code sample.

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.


Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.


Thanks!


Related articles:


Share this article


Generic placeholder image

WRITTEN BY

Gurjit Singh

I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me on twitter @gurjitpt for more updates.


Next Posts

How to use enum in Swift Empty space for void

Enumerations (enum) enables you to write code in a type safe way. Enum is very useful while defining common type of values. You don't have to provide...

Mar 7, 2023 . 2 min read     Swift Enum

Read More »

Optional unwrapping syntax in Swift 5.7 Empty space for void

Optional unwrapping is one of the most used patterns in iOS development. Swift 5.7 introduces new features included a new way to unwrap optional values...

Jun 14, 2022 . 2 min read     Optional unwrapping

Read More »

Property observers in Swift Empty space for void

It's a common pattern or technique in various programming languages when we want to perform some action when a value is changed. Property ...

May 24, 2021 . 2 min read     Property Observers

Read More »