链接`NSOperation`:将结果从一个操作传递到下一个操作 [英] Chaining `NSOperation` : Pass result from an operation to the next one

查看:52
本文介绍了链接`NSOperation`:将结果从一个操作传递到下一个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法来为链接的 NSOperation 传递结果.例如,假设我们链接了 3 个操作:

I've been looking for a way to pass results for chained NSOperation. For example, lets assume we have 3 operations chained:

  1. Operation1 从服务器下载 JSON 数据
  2. Operation2 解析 &收到模型 JSON
  3. Operation3 下载用户图片
  1. Operation1 to download JSON data from server
  2. Operation2 to parse & model JSON received
  3. Operation3 to download user images

所以 Op3 将依赖于 Op2,而 Op2 又依赖于 Op1.但我正在寻找方法将结果从 Op1 -> Op2,然后从 Op2 -> Op3 传递为:

So Op3 would be dependent on Op2, which is dependent on Op1. But I'm looking for way to pass results from Op1 -> Op2, then from Op2 -> Op3 as:

[operation1 startWithURL:url];
[operation2 parseJSONfromOp1IntoModel:JSONData];
[operation3 downloadUserImagesForUser: UserModelObject];

嵌套块似乎不是一个干净易读的解决方案,知道吗?

and nesting blocks doesn't seem to be a clean readable solution, any idea?

推荐答案

如果你想链式操作,但又不喜欢嵌套,可以使用 NSOperation 子类,然后自己定义完成处理程序:

If you want to chain operations, but don't like the nesting, you can use NSOperation subclasses, and then define your own completion handlers:

DownloadOperation *downloadOperation = [[DownloadOperation alloc] initWithURL:url];
ParseOperation *parseOperation = [[ParseOperation alloc] init];
DownloadImagesOperation *downloadImagesOperation = [[DownloadImagesOperation alloc] init];

downloadOperation.downloadCompletionHandler = ^(NSData *data, NSError *error) {
    if (error != nil) {
        NSLog(@"%@", error);
        return;
    }

    parseOperation.data = data;
    [queue addOperation:parseOperation];
};

parseOperation.parseCompletionHandler = ^(NSDictionary *dictionary, NSError *error) {
    if (error != nil) {
        NSLog(@"%@", error);
        return;
    }

    NSArray *images = ...;

    downloadImagesOperation.images = images;
    [queue addOperation:downloadImagesOperation];
};

[queue addOperation:downloadOperation];

坦率地说,我不确定这是否比嵌套方法更直观:

Frankly, though, I'm not sure that's any more intuitive than the nested approach:

DownloadOperation *downloadOperation = [[DownloadOperation alloc] initWithURL:url downloadCompletionHandler:^(NSData *data, NSError *error) {
    if (error != nil) {
        NSLog(@"%@", error);
        return;
    }

    ParseOperation *parseOperation = [[ParseOperation alloc] initWithURL:data parseCompletionHandler:^(NSDictionary *dictionary, NSError *error) {
        if (error != nil) {
            NSLog(@"%@", error);
            return;
        }

        NSArray *images = ...

        DownloadImagesOperation *downloadImagesOperation = [[DownloadImagesOperation alloc] initWithImages:images imageDownloadCompletionHandler:^(NSError *error) {
            if (error != nil) {
                NSLog(@"%@", error);
                return;
            }

            // everything OK
        }];
        [queue addOperation:downloadImagesOperation];
    }];
    [queue addOperation:parseOperation];
}];
[queue addOperation:downloadOperation];

顺便说一下,上面假设您熟悉 NSOperation 的子类化,尤其是创建异步 NSOperation 子类的微妙之处(并执行所有必要的 KVO).如果您需要有关如何完成的示例,请告诉我.

By the way, the above assumes that you're familiar with subclassing NSOperation, especially the subtleties of creating an asynchronous NSOperation subclass (and doing all of the necessary KVO). If you need examples of how that's done, let me know.

这篇关于链接`NSOperation`:将结果从一个操作传递到下一个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆