iOS스터디/Swift

[Swift] GCD Dispatch Queue

슬라임 통통 2021. 3. 3. 18:23
728x90

Main Queue : 시스템이 생성해주는거 사용

사용자 UI update 관련  인터렉션 처리

 

async만 사용가능.

 

 

 

 

Global Queue : 시스템이 생성해주는거 사용

QoS (Quality or Service): 글로벌 큐에서 사용될 우선순위를 정해주는 인자 (우선순위가 높은순)

1. userInteractive : 바로쓰는 대화에 작업에 사용

2. userInitiated 

3. default 

4. utility : 수초~ 수분에 걸리는 사용 (네트워크 파일 등)

5. background : 사용자들과 당장 인터렉션이 필요가 없는 경우 (뉴스, 위치, 영상 다운 등등)

 

 

 

 

Custom Queue : 사용자 직접 생성

직접 큐를 생성해서 쓰는 경우

 

let concurrentQueue = DispatchQueue(label: "concurrent", qos: .background, attributes: .concurrent)
let serialQueue = DispatchQueue(label: "serial", qos: .background)

 

 

 

 

 

 


두 개의 Queue 같이 쓰기

 

예 : 이미지를 다운받고 UI를 업데이트 시켜주어야 하는 경우.

 

Dispatch.global(qos: .background).async{
	let image = downloadImageFromServer()
    DispatchQueue.main.async{
    	self.imageView.image = image
    }
}

 

 

 

 

 


Sync & Async

sync : 앞의 작업이 끝나야 실행될 수 있게 하는 것.

async : 앞의 작업이 끝나지 않아도 실행될 수 있게 하는 것. (대부분의 작업)

 

 

 

 

 

 

 

728x90
반응형