[iOS] NSOperationQueue와 GCD Queue의 차이점
Multi Threading
- GCD(Grand Central Dispath):
C
기반의 low-level
API
- NSOperation:
Objective-C
기반으로 만들어진 high-level
API
NSOperationQueue
- NSOperation 객체의 우선순위 및 준비 상태에 따라 대기열에 있는 객체를 실행한다. Operation Queue에 추가된 작업은 작업이 완료될 때까지 대기열에 남아 있다. 작업이 추가된 후에는 대기열에서 직접 제거할 수 없다.
- 모든 작업이 끝나지 않은 상태에서 OperationQueue를 중단시키면
메모리 릭
이 발생할 수 있다.
- GCD에서는 할 수 없는 기능들 (
재개
, 취소
, 중지
)을 제공하지만 구현이 복잡하고 무겁다
NSOperations
, NSOperationQueue
를 사용할 때 오버헤드가 발생한다.
오버헤드
: 어떤 처리를 하기 위해 들어가는 간접적인 처리 시간, 메모리 등을 말한다.
- KVO(Key-Value Observing) 사용이 가능하다. - GCD는 사용 불가
- oeprations(Read Only) : 현재 큐에 올라가 있는 Tasks
- operationCount(Read Only) : 현재 큐에 있는 작업의 개수
- maxConcurrentOperationCount(Readable and Writable) : 구에서 동시에 실행할 수 있는 작업의 최대 개수
- suspended(Readable and Writable) : 실행 작업을 적극적으로 스케쥴링하고 있는지 여부에 대한 상태값
True/false
로 반환한다.
- name(Readable and Writable) :OperationQueue의 이름
- 작업 간의 의존성
- 어떤 작업을 다른 작업이 성공적으로 수행한 후에 실행할 수 있게 하는 작업의 계층을 만들 수 있다.
GCD Queue
- 동시성 모델을 매우 간단하게 사용할 수 있는 C기반 low-level API이다
- 앱의 메인 스레드 또는 백그라운드 스레드에서 작업의 실행을 직렬 또는 동시에 관리하는 객체이다.
- 작업 항목을 동기 또는 비동기적으로 예약한다
- 동기적 : 여러 작업을 순서대로 실행한다
- 비동기적 : 모든 동기 작업이 완료되기 전에 추가 스레드의 작업을 비동기적으로 실행해서 작업을 병렬적으로 수행하는 방식이다
- 메인 큐에서 작업 항목을 동기적으로 실행하면
교착 상태
가 발생할 수 있다.