Networking in Swift: URLSession and REST APIs Quiz Quiz

Explore key concepts of networking in Swift with questions on URLSession, REST APIs, and related techniques. Enhance your understanding of API requests, JSON parsing, and asynchronous handling in Swift app development.

  1. Understanding URLSession Tasks

    Which URLSession task type is best suited for downloading a large file asynchronously in Swift?

    1. URLSessionStreamTask
    2. URLSessionDataTask
    3. URLSessionDownloadTask
    4. URLSessionUploadTask

    Explanation: URLSessionDownloadTask is designed to handle downloading large files efficiently by writing data directly to a temporary file. URLSessionDataTask is mainly for fetching smaller pieces of data that fit in memory. URLSessionUploadTask is specialized for uploading data rather than downloading. URLSessionStreamTask is intended for bidirectional streaming, making it unsuitable for downloading large files.

  2. Making a Simple GET Request

    Which HTTP method should be specified in a URLRequest for retrieving data from a REST API in Swift?

    1. PUT
    2. POST
    3. SEND
    4. GET

    Explanation: The GET method is used for retrieving information from a server without modifying any data, which is the standard for data-fetching operations. POST is typically used to send data or create resources, not just retrieve information. SEND is not a valid HTTP method. PUT is used for updating or replacing resources on the server.

  3. Decoding JSON Responses

    Which Swift structure can be used to decode JSON data received from a REST API when using Codable?

    1. Decodable
    2. Encodable
    3. Serializable
    4. Descriptive

    Explanation: Decodable is used in Swift to decode JSON data into custom structures or classes, enabling you to parse API responses efficiently. Encodable is meant for encoding data to JSON but not for decoding. Serializable is not a Swift protocol related to JSON parsing. Descriptive is not relevant in this context.

  4. Handling Completion Handlers

    What parameter type is typically used in the completion handler of a URLSessionDataTask in Swift?

    1. String, Int, Float
    2. Data?, URLResponse?, Error?
    3. Int?, String?, Bool?
    4. Array, Dictionary, Bool

    Explanation: A URLSessionDataTask completion handler receives three parameters: Data?, URLResponse?, and Error?, which represent the response data, metadata, and potential error. The other options list types not used together in this context, such as String, Int, and Float or collections like Array and Dictionary.

  5. Configuring URLSession

    Which class is used to configure background downloads with URLSession in Swift?

    1. URLSessionConfiguration
    2. SessionManager
    3. DownloadManager
    4. BackgroundSession

    Explanation: URLSessionConfiguration is utilized to set up various session behaviors, including background downloads, by specifying its 'background' option. SessionManager is not a standard class for configuration in this context. BackgroundSession and DownloadManager are not built-in Swift classes or configuration tools.

  6. Setting HTTP Request Headers

    Which method allows you to set an HTTP header value for a URLRequest in Swift?

    1. addHTTPHeader(_:)
    2. setHeader(_:withValue:)
    3. updateHeaderField(_:value:)
    4. setValue(_:forHTTPHeaderField:)

    Explanation: The setValue(_:forHTTPHeaderField:) method directly sets a value for a specific HTTP header in a URLRequest. The other options, such as addHTTPHeader(_:), setHeader(_:withValue:), and updateHeaderField(_:value:), are not valid methods in the URLRequest API.

  7. Understanding REST API Status Codes

    What is the typical HTTP status code to check for a successful network response when using URLSession in Swift?

    1. 200
    2. 500
    3. 404
    4. 301

    Explanation: Status code 200 indicates a successful response from the server, meaning the request was handled correctly. 404 signals the resource wasn't found, 500 points to a server error, and 301 signifies a resource has permanently moved, none of which indicate success.

  8. Performing API Requests Asynchronously

    Why is it important to execute URLSession network requests on a background thread in Swift?

    1. To keep the main UI thread responsive
    2. To store data locally
    3. To increase download speeds
    4. To lower battery usage

    Explanation: Running network requests on a background thread ensures the app interface remains responsive and prevents freezing or lag. Faster download speeds are not a direct result of threading. Lowering battery usage and storing data locally are unrelated to the need for background network execution.

  9. Sending JSON Data to a REST API

    When making a POST request to submit JSON data using URLSession, which property should you set on the URLRequest?

    1. bodyStream
    2. requestBody
    3. dataSource
    4. httpBody

    Explanation: The httpBody property of URLRequest holds the data sent with the request, such as JSON for POST requests. bodyStream is an alternative for streaming data but is less common for simple JSON. requestBody and dataSource are not valid URLRequest properties.

  10. Detecting Networking Errors

    In the URLSession completion handler, which parameter should you check first to safely handle errors?

    1. info
    2. response
    3. error
    4. data

    Explanation: You should first check the error parameter, as its presence indicates network failures or issues with the request. The data and response parameters might be nil or incomplete if an error occurred. info is not a parameter provided in the completion handler.