Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <pre><code>- (RACSignal *)asyncOperation { return [RACSignal createSignal:^RACDisposable * (id&lt;RACSubscriber&gt; subscriber) { [self asyncOperationWithCompletion:^{ [subscriber sendCompleted]; }]; return nil; // `nil` means there's no way to cancel. }]; } </code></pre> <p>EDIT: Thanks to the comment by Justin Spahr-Summers, here is a much simpler approach to chaining the operations:</p> <pre><code>RACSignal *signal = [RACSignal concat:[array.rac_sequence map:^(id entity) { return [entity asyncOperation]; }]]; </code></pre> <p>ReactiveCocoa's <code>+concat:</code> 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 <code>-rac_sequence</code> here is for the purpose of mapping the entities to the operation signals.</p> <p>In this context, the use of <code>+concat:</code> achieves the same effect as the <code>-then:</code> chaining from my initial answer below.</p> <hr> <p>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 <a href="https://github.com/ReactiveCocoa/ReactiveCocoa/blob/f9bc1fd2c58e0c5f9096a1c42d3e51d9c912d4e0/ReactiveCocoaFramework/ReactiveCocoa/RACSignal+Operations.h#L196-205" rel="noreferrer"><code>-then:</code></a>. The <code>-then:</code> operation essentially waits for the previous operation to complete before beginning the next.</p> <pre><code>RACSignal *signal = [RACSignal empty]; for (id entity in array) { signal = [signal then:^{ return [entity asyncOperation]; }]; } [signal subscribeCompleted:^{ // At this point, all operations have completed }]; </code></pre> <p>At this point what you have is:</p> <pre><code>[[[[RACSignal empty] then:^{ return [entity1 asyncOperation]; }] then:^{ return [entity2 asyncOperation]; }] // ... then:^{ return [entityN asyncOperation]; }] </code></pre> <p>ReactiveCocoa has helpful <a href="https://github.com/ReactiveCocoa/ReactiveCocoa/tree/master/Documentation" rel="noreferrer">documentation</a> and very well documented headers, both of which were very valuable to me when I was new.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload