Skip to content

Latest commit

 

History

History
143 lines (102 loc) · 9.48 KB

File metadata and controls

143 lines (102 loc) · 9.48 KB

Unit 3: Networking, Concurrency, APIs

Lessons, Quizzes, and Labs

Lesson Quiz Lab
1. Introduction to the Internet and Networking 6.1 / 6.3 API and Status Code Scavenger Hunt
2. Parsing JSON 6.1 / 6.3 Weather / Color / Random User
3. Unit Testing 6.1 / 6.3 Jokes / Star Wars / Trivia
4. Getting Data from Online 6.1 / 6.3 n/a
5. Concurrency and Grand Central Dispatch 6.1 / 6.3 Country List
6. Images and Error Handling 6.1 / 6.3 XCKD / Pokemon / Random User
7. Retain Cycles 6.1 / 6.3 n/a
8. API Keys and Auth 6.1 / 6.3 Musixmatch
9. Post Requests 6.1 / 6.3 Airtable Clients List

New Standards

  • EF.6: Write unit tests to ensure that software works as intended
  • EF.10.f: Use Codable and JSONDecoder to parse JSON data
  • EF.10.g: Use GCD to appropriately dispatch tasks
  • EF.10.h: Use URLSession to get data from online
  • IOS.3.e: Use a search bar to filter data in a Table View.
  • iOS.3.s: Load images smoothly, without flickering , and displaying a spinner when needed

Reassessed Standards

  • EF.4.e: Use frequent, descriptive, small commits
  • EF.8.a: Create an App using MVC design
  • IOS.1: Write clean, readable Swift code
  • IOS.2.g: Auto_Layout
  • IOS.3.d: Load Data into a TableView
  • LF.5: Use functions to keep code DRY
  • iOS.3.p: Create Custom TableView Cells
  • iOS.3.q: Segue to a DetailViewController
  • iOS.3.r: Create a TableView with Sections

Assignments

Assignment Standards
Stocks and Contacts
  • EF.4.e: Use frequent, descriptive, small commits
  • EF.6: Write unit tests to ensure that software works as intended
  • EF.10.f: Use Codable and JSONDecoder to parse JSON data
  • IOS.1: Write clean, readable Swift code
  • IOS.2.g: Auto_Layout
  • IOS.3.d: Load Data into a TableView
  • IOS.3.e: Use a search bar to filter data in a Table View.
  • LF.5: Use functions to keep code DRY
  • iOS.3.q: Segue to a DetailViewController
  • iOS.3.r: Create a TableView with Sections
TV Shows
  • EF.4.e: Use frequent, descriptive, small commits
  • EF.6: Write unit tests to ensure that software works as intended
  • EF.8.a: Create an App using MVC design
  • EF.10.f: Use Codable and JSONDecoder to parse JSON data
  • EF.10.g: Use GCD to appropriately dispatch tasks
  • EF.10.h: Use URLSession to get data from online
  • IOS.1: Write clean, readable Swift code
  • IOS.2.g: Auto_Layout
  • IOS.3.d: Load Data into a TableView
  • IOS.3.e: Use a search bar to filter data in a Table View.
  • LF.5: Use functions to keep code DRY
  • iOS.3.q: Segue to a DetailViewController
  • iOS.3.s: Load images smoothly, without flickering , and displaying a spinner when needed

Assessment

Assessment Standards
6.1 Elements / 6.3 Elements
  • EF.4.e: Use frequent, descriptive, small commits
  • EF.6: Write unit tests to ensure that software works as intended
  • EF.8.a: Create an App using MVC design
  • EF.10.f: Use Codable and JSONDecoder to parse JSON data
  • IOS.1: Write clean, readable Swift code
  • IOS.2.g: Auto_Layout
  • IOS.3.d: Load Data into a TableView
  • iOS.3.p: Create Custom TableView Cells
  • iOS.3.q: Segue to a DetailViewController
  • iOS.3.s: Load images smoothly, without flickering , and displaying a spinner when needed

Source Code and Lectures

6.1 Unit 3 Lecture Code

Lecture Videos

Github Cheat Sheet

Extra Content

  1. Singleton Pattern
  2. Working with Dates
  3. App Transport Security

Helper Classes written in Unit 3 to handle Networking

Network Helper - wrapper for URLSession
import Foundation

enum HTTPMethod: String {
    case get = "GET"
    case post = "POST"
}

class NetworkHelper {

    // MARK: - Static Properties

    static let manager = NetworkHelper()

    // MARK: - Internal Properties

    func performDataTask(withUrl url: URL,
                         andHTTPBody body: Data? = nil,
                         andMethod httpMethod: HTTPMethod,
                         completionHandler: @escaping ((Result<Data, AppError>) -> Void)) {
        var request = URLRequest(url: url)
        request.httpMethod = httpMethod.rawValue
        request.httpBody = body
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")

        urlSession.dataTask(with: request) { (data, response, error) in
            DispatchQueue.main.async {
                guard let data = data else {
                    completionHandler(.failure(.noDataReceived))
                    return
                }

                guard let response = response as? HTTPURLResponse, (200...299) ~= response.statusCode else {
                    completionHandler(.failure(.badStatusCode))
                    return
                }

                if let error = error {
                    let error = error as NSError
                    if error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet {
                        completionHandler(.failure(.noInternetConnection))
                        return
                    } else {
                        completionHandler(.failure(.other(rawError: error)))
                        return
                    }
                }
                completionHandler(.success(data))
            }
            }.resume()
    }

    // MARK: - Private Properties and Initializers

    private let urlSession = URLSession(configuration: URLSessionConfiguration.default)

    private init() {}
}
AppError - handles error throughout the app
import Foundation

enum AppError: Error {
    case unauthenticated
    case invalidJSONResponse
    case couldNotParseJSON(rawError: Error)
    case noInternetConnection
    case badURL
    case badStatusCode
    case noDataReceived
    case notAnImage
    case other(rawError: Error)
}