-3

This is a code snippet from Ray Wenderich's iOS 7 Best Practices article. He's basically fetching a JSON file and making a model for each in the list;

return [[self fetchJSONFromURL:url] map:^id(NSDictionary *json) {
                RACSequence *list = [json[@"list"] rac_sequence];

                return [[list map:^(NSDictionary *item) {
                    return [MTLJSONAdapter modelOfClass:[WXDailyForecase class] fromJSONDictionary:item error:nil];
                }] array];
    }];

What does that array do?

Sreejith Ramakrishnan
  • 1,332
  • 2
  • 13
  • 22
  • 3
    It evaluates the `RACSequence` and returns an `NSArray` with the objects (think of it as the `NSEnumerator`'s `-allObjects`) – Alladinian May 29 '14 at 16:02
  • 1
    http://cocoadocs.org/docsets/ReactiveCocoa/2.3.1/Classes/RACSequence.html#//api/name/array – Ian Henry May 29 '14 at 17:42

1 Answers1

0

RACSequence's instance method map returns another RACSequence instance. So if you want a NSArray instance, you have to use the -[RACSequence array] method to evaluate the entire sequence and convert its values into an NSArray.

By the way, the map method is executed lazily; as the sequence produces values, you will receive them one by one until you unsubscribe or reach the end of the sequence. But calling the -array method will block the current thread if all of the values are not yet available (until they all become available, at which point the method will un-block and return the array).

erikprice
  • 6,240
  • 3
  • 30
  • 40
WilliamZang
  • 300
  • 2
  • 10