【问题标题】:How can i set the priority in NSOperationQueue to execute the next operation until the previous is finished?如何在 NSOperationQueue 中设置优先级以执行下一个操作,直到上一个操作完成?
【发布时间】:2012-04-24 00:16:35
【问题描述】:

如何在 NSOperationQueue 中设置优先级以在上一个操作完成之前不执行下一个操作?

   NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
  [operationQueue setMaxConcurrentOperationCount:1];
   NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(addPhotoWithButton:) object:button];
  [operationQueue addOperation:operation];
  [operation release];

【问题讨论】:

  • 这句话令人困惑,我怀疑您的意思是“如何在 NSOperationQueue 中设置优先级以在前一个操作完成之前不执行下一个操作?”。注意添加的“不”。如果确实如此,那么您应该更新您的问题。

标签: iphone cocoa-touch ipad nsoperation nsoperationqueue


【解决方案1】:

假设您的意思是要连续执行操作或一次执行一个操作,那么我认为您的问题是您使用 1 调用 setMaxConcurrentOperationCount: ,然后通过用另一个操作队列覆盖它来丢失对该操作队列的引用.所以你所做的是正确的,你只需要将操作添加到具有所需操作计数的队列中。您可能不是故意这样做的,事实上,我认为您会看到编译器警告。试试这个代码:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:1];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(addPhotoWithButton:) object:button];
[operationQueue addOperation:operation];
[operation release];

【讨论】:

    猜你喜欢
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2020-08-27
    • 2015-01-02
    相关资源
    最近更新 更多