Print and make sense of Task.currentPriority

Hi,

Questions

  1. How I can get determine the current priority of the task. (Task.currentPriority). I don't know how to make sense out of it, printing prints the rawValue.
  2. What is the order of priority (I couldn't find documentation on the order, lowest to highest)?

My attempt:

Is there a better way to determine the priority?

Task {
    print("current priority = \(Task.currentPriority)")
    
    switch Task.currentPriority {
    case .userInitiated:
        print("userInitiated")
    case .high:
        print("high")
    case .medium:
        print("medium")
    case .low:
        print("low")
    case .background:
        print("background")
    default:
        print("something else")
    }
}

There's actually only four priority levels -- userInitiated is just an alias for high -- and the raw values correspond to the priority order:

  • high = 25
  • medium = 21
  • low = 17
  • background = 9

I played around with raw values in-between these options, and while it does let you construct them, trying to spawn a task with a priority of e.g. 11 will crash at runtime.

1 Like

@bbrk24 Thanks a lot for the clarification.

Is there any documentation for this? I wish this was made more explicit.

That's fair points I think, I've opened up an issue to track this: TaskPriority should gain CustomStringConvertible, and docs for properties · Issue #62430 · apple/swift · GitHub

1 Like

@ktoso Thanks a lot!!! Really appreciate the effort!

1 Like