objective-c - 学习NSBlockOperation

标签 objective-c ios nsoperation nsoperationqueue nsblockoperation

我是 block 的忠实粉丝,但没有将它们用于并发。经过一些谷歌搜索后,我拼凑了这个想法,将我学到的所有东西都隐藏在一个地方。目标是在后台执行一个 block ,当它完成时,执行另一个 block (如 UIView 动画)...

- (NSOperation *)executeBlock:(void (^)(void))block completion:(void (^)(BOOL finished))completion {

    NSOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:block];

    NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
        completion(blockOperation.isFinished);
    }];

    [completionOperation addDependency:blockOperation];
    [[NSOperationQueue mainQueue] addOperation:completionOperation];    

    NSOperationQueue *backgroundOperationQueue = [[NSOperationQueue alloc] init];
    [backgroundOperationQueue addOperation:blockOperation];

    return blockOperation;
}

- (void)testIt {

    NSMutableString *string = [NSMutableString stringWithString:@"tea"];
    NSString *otherString = @"for";

    NSOperation *operation = [self executeBlock:^{
        NSString *yetAnother = @"two";
        [string appendFormat:@" %@ %@", otherString, yetAnother];
    } completion:^(BOOL finished) {
        // this logs "tea for two"
        NSLog(@"%@", string);
    }];

    NSLog(@"keep this operation so we can cancel it: %@", operation);
}

我的问题是:

  1. 当我运行它时它可以工作,但我是否遗漏了什么……隐藏的地雷?我还没有测试取消(因为我还没有发明一个长操作),但这看起来行得通吗?
  2. 我担心我需要限定我的 backgroundOperation 声明,以便我可以在完成 block 中引用它。编译器没有提示,但是否有一个保留循环潜伏在那里?
  3. 如果“字符串”是一个 ivar,如果我在 block 运行时观察到它的键值会发生什么?或者在主线程上设置一个计时器并定期记录它?我能看到进展吗?我会声明它是原子的吗?
  4. 如果这按我预期的那样工作,那么这似乎是隐藏所有细节并获得并发性的好方法。为什么 Apple 不为我写这个?我错过了什么重要的东西吗?

谢谢。

最佳答案

我不是 NSOperation 或 NSOperationQueues 方面的专家,但我认为下面的代码要好一些,尽管我认为它仍然有一些注意事项。对于某些目的来说可能足够了,但不是并发的通用解决方案:

- (NSOperation *)executeBlock:(void (^)(void))block
                      inQueue:(NSOperationQueue *)queue
                   completion:(void (^)(BOOL finished))completion
{
    NSOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:block];
    NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
        completion(blockOperation.isFinished);
    }];
    [completionOperation addDependency:blockOperation];

    [[NSOperationQueue currentQueue] addOperation:completionOperation];
    [queue addOperation:blockOperation];
    return blockOperation;
}

现在让我们使用它:

- (void)tryIt
{
    // Create and configure the queue to enqueue your operations
    backgroundOperationQueue = [[NSOperationQueue alloc] init];

    // Prepare needed data to use in the operation
    NSMutableString *string = [NSMutableString stringWithString:@"tea"];
    NSString *otherString = @"for";

    // Create and enqueue an operation using the previous method
    NSOperation *operation = [self executeBlock:^{
        NSString *yetAnother = @"two";
        [string appendFormat:@" %@ %@", otherString, yetAnother];
    }
    inQueue:backgroundOperationQueue 
    completion:^(BOOL finished) {
        // this logs "tea for two"
        NSLog(@"%@", string);
    }];

    // Keep the operation for later uses
    // Later uses include cancellation ...
    [operation cancel]; 
}

您问题的一些答案:

  1. 取消。通常你将 NSOperation 子类化,这样你就可以检查 self.isCancelled 并提前返回。参见 this thread ,就是一个很好的例子。在当前示例中,您无法检查操作是否已从您提供的 block 中取消以进行 NSBlockOperation,因为那时还没有这样的操作。在调用 block 时取消 NSBlockOperation 显然是可能的,但是 cumbersome . NSBlockOperation 用于特定的简单情况。如果你需要取消,你最好继承 NSOperation :)

  2. 我看不出有什么问题。尽管请注意两件事。 a) 我更改了方法 do 以在当前队列中运行完成 block b) 需要一个队列作为参数。正如@Mike Weller 所说,你应该更好地提供后台队列,这样你就不需要为每个操作创建一个,并且可以选择使用哪个队列来运行你的东西:)

    <
  3. 我认为是的,您应该使 string atomic。您不应忘记的一件事是,如果您向队列提供多个操作,它们可能不会(必然)按该顺序运行,因此您可能会在 string 中得到一条非常奇怪的消息。如果您需要一次连续运行一个操作,您可以在开始对操作进行排队之前执行:[backgroundOperation setMaxConcurrentOperationCount:1];docs 中有一个值得阅读的注释虽然:

    Additional Operation Queue Behaviors An operation queue executes its queued operation objects based on their priority and readiness. If all of the queued operation objects have the same priority and are ready to execute when they are put in the queue—that is, their isReady method returns YES—they are executed in the order in which they were submitted to the queue. For a queue whose maximum number of concurrent operations is set to 1, this equates to a serial queue. However, you should never rely on the serial execution of operation objects. Changes in the readiness of an operation can change the resulting execution order.

  4. 我想看完这些你就知道了:)

关于objective-c - 学习NSBlockOperation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11009027/

相关文章:

ios - 设置 slider 值

iphone - iOS6 : do we have to set rootViewController in App delegate in order to support different orientations?

objective-c - 如何在 Objective-C 中使用 NSOperation 和 NSOperationQueue?

ios - 了解 Swift 中具有依赖性的操作顺序

ios - 无法将 cookie 发送到 WebView Objective C

objective-c - 比较 Swift 中的 AnyObject

iphone - 使用 NSXMLParser 获取第一个 <created_at> 条目以显示推文的日期

ios - 每次从 SwiftUI 中的详细 View 返回时,如何阻止 View 刷新?

ios - 在 uiScrollView 之前添加 uiTextView

cocoa - NSOperationQueue 不在主线程上?