- DispatchQueue도 커스텀하여 사용할 수 있음
- label을 제외한 파라미터들은 기본값으로 설정되어 있기 때문에 필요한 파라미터만 초기화해줄 수 있음
convenience init(label: String,
qos: DispatchQoS = .unspecified,
attributes: DispatchQueue.Attributes = [],
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency = .inherit,
target: DispatchQueue? = nil)
1️⃣ label
let derrickDispatchQueue = DispatchQueue(label: "derrick")
- DispathQueue의 label을 설정해주는 파라미터
- 디버깅 환경에서 추적하기 위해 작성하는 String 값임
- 식별자와 같음
2️⃣ qos
- DispatchQoS 타입의 값을 받는 파라미터
- Quality of Service의 약자
- 실행될 Task들의 우선 순위를 정해주는 값
3️⃣ attributes
- DispatchQueue의 속성을 정해주는 값
- .concurrent로 초기화한다면 다중 스레드 환경에서 코드를 처리하는 DispatchQueue가 됨
- 빈 배열 → 기본 값
- .initiallyInactive
- DispatchQueue의 sync, async 작업을 제어해줌
- sync, async를 호출하더라도 작업을 큐에 담아놓을 뿐 active()를 호출하기 전까지는 작업을 처리하지 않는 것
import Foundation
let yellow = DispatchWorkItem {
for _ in 1...5 {
print("😀😀😀😀😀")
sleep(1)
}
}
let myDispatch = DispatchQueue(label: "Odong", attributes: .initiallyInactive)
myDispatch.async(execute: yellow) // 코드 블록 호출 안됨.
myDispatch.activate()
4️⃣ autoreleaseFrequency
- 자동으로 객체를 해제하는 빈도의 값을 결정하는 파라미터