5

현재 GCD를 사용 중입니다. 그러나, 나는 NSOperation가 실제로 더 높은 수준의 프로그램이라고 들었다. 그래도 훨씬 더 복잡합니다. 간단어느 것이 더 사용하기 쉽습니까? GCD 또는 NSOperation?

+(void)doForeGround:(void (^)())block 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     block(); 
    }); 
} 

+(void)doBackground:(void (^)())block 
{ 

    //DISPATCH_QUEUE_PRIORITY_HIGH 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0), ^{ 
    //dispatch_async(dispatch_get_global_queue(-2,0), ^{ 
     block(); 
    }); 
} 

-(void)doBackGround:(void (^)())block onComletion:(void (^)())onCompletion 
{ 
    [BGHPTools doBackground:^{ 
     block(); 
     [BGHPTools doForeGround:^{ 
      onCompletion(); 
     }]; 
    }]; 
} 

NSOperation으로 그 일 : GCD에서

, 내가 만든이 도우미 기능을 사용하여 간단하게 배경에서 뭔가를하면된다?

내가 누락 된 항목이 있습니까? NS 운영에서 어떻게해야합니까?

답변

6

당신이 GCD로하고있는 것처럼 당신은 NSOperation와 비슷한 일을 할 수있다. 가장 큰 차이점은 NSOperation이 추가 기능을 제공한다는 것입니다. 예를 들어

는 :

  • NSOperation-cancel 방법이있다. 디스패치 대기열에는 취소의 개념이 없습니다. 대기열에 대기중인 모든 블록이 완료 될 때까지 실행됩니다.
  • NSOperationQueue에는 maximumConcurrentOperationCount 속성이 있습니다.이 속성을 사용하여 한 번에 3 개의 작업 만 실행할 수 있습니다. 디스패치 대기열에는 이러한 개념이 없습니다. 그들은 한 번에 한 블록 씩 허용하거나, 동시 사용이 가능하며, CPU 사용 및 가용성에 따라 libdispatch을 권장합니다.
  • NSOperation은 다른 모든 종속성이 실행될 때까지 특정 작업의 실행을 지연 할 수 있도록 다른 종속성을 가질 수 있습니다. 다른 작업은 종속 작업이 대기하는 동안 대기열에서 "뛰어 오르는"것이 허용됩니다. 디스패치 대기열은 항상 FIFO 순서로 대기열에서 제외됩니다. (당신은 다소 dispatch_group API를 사용하여 종속성을 모방 할 수 있지만, 정말 문제의 다른 종류의 대상입니다.)

를 이제, 당신이 그 기능 중 하나를 사용하지 않는 경우, GCD는 잘 작동합니다. 은 (는)이 아니며 GCD 자체를 사용하고 있습니다. NSOperation이 몇 가지 추가 기능을위한 편리한 래퍼를 제공합니다. 여기

NSOperationQueue를 사용하여 위의 예를 다시 줄 방법은 다음과 같습니다

+(void)doForeground:(void (^)())block 
{ 
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
     NSLog(@"I'm running on the main thread!"); 
     block(); 
    }]; 
} 

+(void)doBackground:(void (^)())block 
{ 
    // Note; rather than allocating a new NSOperationQueue every time, you could 
    // allocate the queue once and just refer to that queue. For simplicity, I'll 
    // skip that here. 
    [[NSOperationQueue new] addOperationWithBlock:^{ 
     NSLog(@"I'm running asynchronously on whatever thread NSOperationQueue wants!"); 
     block(); 
    }]; 
} 

-(void)doBackground:(void (^)())block onCompletion:(void (^)())onCompletion 
{ 
    [[NSOperationQueue new] addOperationWithBlock:^{ 
     block(); 
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
      onCompletion(); 
     }]; 
    }]; 
} 
+0

그리고 NSOperation도 자체적으로 중첩 된 autorelease 스택을 만들겠습니까? –

+0

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html에는 new라는 메소드가 설명되어 있지 않습니다. 아, alloc의 바로 가기가 있어야합니다] init –

+0

'+ new'는 alloc과 init처럼'NSObject'에서 상속되었습니다. 그리고 네, 각 작업 블록은 자동 복구 풀에 살 것입니다. –

4

당신의 비교를위한 작업 큐 예 :

- (void)viewDidLoad 
{ 
    // ... 

    self.queue = [[NSOperationQueue alloc] init]; 

    // ... 
} 

- (void)job 
{ 
    // background activities ... 
} 

- (void)startJob 
{ 
    NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self 
                    selector:@selector(job) 
                    object:nil]; 
    [self.queue addOperation:op]; 
} 
+1

어쨌든 블록 : 물론 하나의 –

+1

를 사용하여 좋아한다. –

관련 문제