使用ReactiveCocoa链接来自对象数组的异步操作 [英] Chaining asynchronous operations from an array of objects with ReactiveCocoa

查看:131
本文介绍了使用ReactiveCocoa链接来自对象数组的异步操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组实体,我想对实体执行异步操作。操作应链接并以与阵列中的实体相同的顺序运行。我是RAC的新手。如何在RAC中执行此操作?

I have an array of entities and I want to perform asynchronous operations on the entities. The operations should be chained and run in the same order with the entities in the array. I'm new to RAC. How to do that in RAC?

推荐答案

首先,您需要一个执行异步操作的包装器方法,它将返回一个信号。假设异步操作操作采用完成块。从它的声音,你不关心值,你想要副作用,在这种情况下信号不发送值,它只完成。

First, you'll need a wrapper method that performs your async operation, which will return a signal. Let's assume the async operation operation takes a completion block. From the sounds of it, you don't care about the values, you want the side effects, in which case the signal does not send values, it only completes.

- (RACSignal *)asyncOperation {
    return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {
        [self asyncOperationWithCompletion:^{
            [subscriber sendCompleted];
        }];
        return nil; // `nil` means there's no way to cancel.
    }];
}

编辑:感谢Justin Spahr-Summers的评论,这里有很多更简单的链接操作的方法:

Thanks to the comment by Justin Spahr-Summers, here is a much simpler approach to chaining the operations:

RACSignal *signal = [RACSignal concat:[array.rac_sequence map:^(id entity) {
    return [entity asyncOperation];
}]];

ReactiveCocoa的 + concat:运算符需要一个集合信号和一次一个地订阅信号,在订阅其后继者之前等待一个信号的完成。这里使用 -rac_sequence 是为了将实体映射到操作信号。

ReactiveCocoa's +concat: operator takes a collection of signals and subscribes to the signals one at a time, waiting for completion of one signal before subscribing to its successor. Using -rac_sequence here is for the purpose of mapping the entities to the operation signals.

在这种情况下,使用 + concat:可以达到与 -then:链接相同的效果。

In this context, the use of +concat: achieves the same effect as the -then: chaining from my initial answer below.

使用RAC包装器,解决方案是从空信号开始,通过迭代实体并组装使用 进行的操作 - 然后 -then:操作基本上等待上一个操作在开始下一个操作之前完成。

With the RAC wrapper, a solution is to start with an empty signal, and build a chain by iterating over the entities and assembling the operations using -then:. The -then: operation essentially waits for the previous operation to complete before beginning the next.

RACSignal *signal = [RACSignal empty];
for (id entity in array) {
    signal = [signal then:^{
        return [entity asyncOperation];
    }];
}

[signal subscribeCompleted:^{
    // At this point, all operations have completed
}];

此时你所拥有的是:

[[[[RACSignal empty]
    then:^{ return [entity1 asyncOperation]; }]
    then:^{ return [entity2 asyncOperation]; }]
    // ...
    then:^{ return [entityN asyncOperation]; }]

ReactiveCocoa有帮助文档以及非常好的文档标题,这对我来说非常有价值。

ReactiveCocoa has helpful documentation and very well documented headers, both of which were very valuable to me when I was new.

这篇关于使用ReactiveCocoa链接来自对象数组的异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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