Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save linearhw/889f2a3fa4f6e96a63a8396e20c52400 to your computer and use it in GitHub Desktop.
Save linearhw/889f2a3fa4f6e96a63a8396e20c52400 to your computer and use it in GitHub Desktop.
Effective Objective-C 43

사실 Operation Queue 는 GCD 를 내부적으로 사용하고 있다.

GCD 와 Operation Queue 의 차이

  • GCD 는 C API 이다. Operation Queue 는 Obj-C 객체다.
  • GCD 큐 안에 있는 작업은 블록이다 (가볍다). Operation 은 Obj-C 객체다 (무겁다).

하지만 이럴 때는 Operation Queue 가 더 낫다.

  1. 작업 취소
    Operation Queue 의 경우, 대기 중인건 취소 가능
    GCD의 경우 cancel 기능이 없다. 직접 구현하는 수밖에..
  2. 의존 작업
    반드시 A가 성공적으로 실행된 뒤에 B가 실행되어야 할 때
    Operation 은 원하는 만큼 다른 Operation 에 의존할 수 있다.
  3. Operation 프로퍼티의 KVO
    Operation 은 isCancelled 나 isFinished 등의 프로퍼티를 갖고 있어서 KVO를 사용하면 GCD보다 세세한 제어가 가능하다.
  4. 작업 우선순위
    각 Operation 은 우선순위가 있다. 우선순위가 높은 Operation 이 낮은 것보다 먼저 실행된다.
    GCD는 같은 일을 하는 직접적인 방법이 없다. 큐 우선순위 밖에 존재하지 않는다.
  5. 작업의 재사용
    직접 Operation Queue 를 상속받아서 하위 클래스를 만들 수 있고, 코드 내에서 재사용할 수도 있다.

실제로 Operation Queue 를 사용하는 경우

  • NSNotificationCenter. 선택자를 호출하는 대신 블록으로 알림을 받을 수 있다.
- (id)addObserverForName:(NSString *)name
                  object:(id)object
                   queue:(NSOperationQueue *)queue
              usingBlock:(void(^)(NSNotification *))block
  • 여기서 Operation Queue 말고 Dispatch Queue 를 사용하면 불필요한 GCD에 대한 의존성이 생긴다.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment