Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Last active November 27, 2019 03:15
Show Gist options
  • Save alexpaul/6dec249972a5250683ab8543dfede25c to your computer and use it in GitHub Desktop.
Save alexpaul/6dec249972a5250683ab8543dfede25c to your computer and use it in GitHub Desktop.

Networking in iOS (Terminology)

  1. JSON - web api formatted data the is the most popular way of serving downloaded or uploaded content from a network resource
    {
      "language": "swift", 
      "releaseYear": 2014, 
      "url": "https://developer.apple.com/swift/images/swift-og.png"
    }
  2. Codable (Decodable, Encodable) - introduced in Swift 4.2 this protocol allows us to convert JSON into Swift objects quite easily compared to its predecessor JSONSerialization
    struct ProgrammingLanguage: Codable {
      let language: String 
      let releaseYear: Int
      let url: String
    }
  3. JSONDecoder() - class that converts JSON data to a swift object. The swift object must conform to the Codable protocol.
    let headlinesData = try JSONDecoder().decode(HeadlinesData.self, from: data)
  4. URL - a path to a local or network resource
    URL(string: urlStirng)
  5. URLSession - class used to initiate a network request for a resource, used to upload or download web content
    let dataTask = URLSession.shared.dataTask(with: url) { (data, response, error) in
      // code here
    }
    dataTask.resume()
     
  6. Result type - an enum that is used in a completion handler when making a network request. The enum cases are success and failure.
    (Result<UIImage?, Error>) -> ()
  7. Grand Central Dispatch (GCD) - the concurrency API in Swift. Concurrency is the act of many things happening simultaneously. e.g in iOS that would be allowing a table view to be scrolled without interruption to the user of the application while images are being fetched from the network.
    DispatchQueue.main.async {
      // UI updates go here
      self.headlineImageView.image = image
    }
  8. Escaping Closures - a completion handler needs to be marked @escaping due to the fact that the function returns before the closure value is set from typically an asychronous call (example a background task from fetching a network resource)
    completion: @escaping (Result<UIImage?, Error>) -> ()
  9. Capture list - [weak self] or [unowned self] - used to break retain cycles. Having a retain cycle leads to memory leaks and eventual crashes in our applications.
    { [unowned self] (result) in
       // code 
    }
  10. RESTFul API - uses http methods e.g POST, GET, PUT, DELETE to communicate with a web API (Application Programming Interface). Below is an example of an endpoint from the itunes search RESTFul API performing a GET request to retrieve podcast with the term being "swift" GET https://itunes.apple.com/search?media=podcast&limit=200&term=swift

...more to come

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment