Câu hỏi phỏng vấn Swift
Câu hỏi

What is QoS (Quality...

Câu trả lời

Quality of Service (QoS) in Grand Central Dispatch (GCD) is a mechanism that allows developers to categorize and prioritize tasks based on their importance and urgency. This categorization helps the system allocate resources efficiently, ensuring that more critical tasks receive the necessary CPU time and other resources to complete promptly, while less critical tasks are handled with lower priority.

QoS Classes

There are several QoS classes in GCD, each designed for different types of tasks:

  1. User-Interactive:

    • Purpose: For tasks that need to be executed immediately to ensure a smooth user experience, such as animations or UI updates.
    • Priority: Highest priority.
    • Example: Updating the UI in response to user actions.
  2. User-Initiated:

    • Purpose: For tasks initiated by the user that need to be completed quickly but are not necessarily tied to immediate UI updates.
    • Priority: High priority.
    • Example: Loading data from a network request that the user is waiting for.
  3. Utility:

    • Purpose: For tasks that take a bit longer to complete and do not need to be tracked by the user.
    • Priority: Medium priority.
    • Example: Downloading files in the background or performing calculations.
  4. Background:

    • Purpose: For tasks that can run in the background and are not time-sensitive.
    • Priority: Lowest priority.
    • Example: Saving data to a database or performing maintenance tasks.
  5. Default:

    • Purpose: For tasks that do not have a specific QoS assigned.
    • Priority: Falls between User-Initiated and Utility.
    • Example: General-purpose tasks that do not fit into the other categories.
  6. Unspecified:

    • Purpose: Represents the absence of QoS information.
    • Priority: The system infers the QoS based on the context.
    • Example: Legacy code that does not specify a QoS.

Applying QoS in Code

QoS can be applied to dispatch queues and blocks in GCD. Here are some examples in Swift:

swift Copy
// Creating a queue with a specific QoS
let queue = DispatchQueue(label: "com.example.myqueue", qos: .userInitiated)

// Dispatching a task with a specific QoS
queue.async(qos: .background) {
    print("...
senior

senior

Gợi ý câu hỏi phỏng vấn

middle

What is the difference between a computed property and a property set to a closure?

middle

What's the difference between Self vs self ?

middle

Explain the defer usage in Swift

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào