how to get item from rac_sequence, reactive cocoa, ios

729 views Asked by At

I am parsing the needed links which is in the header of the response from the server. My header looks like

Access-Control-Allow-Origin → * Age → 0 Cache-Control → private,must-revalidate

Connection → keep-alive

Content-Encoding → gzip Content-Type → application/json

Date → Sat, 13 Jun 2015 15:58:56 GMT ETag → W/"cb38bb07f1635fd6aba5969985bf0607"

Link → http://thisIsCurrentlink&limit=24; rel="next",http://thisIsLastlink&limit=24; rel="last",http://thisIsFirstlink&limit=24; rel="first",<>; rel="prev"

Server → nginx

Vary → Accept-Encoding

X-Total-Count → 131

transfer-encoding → chunked

By doing this, I can get the links array which contains all links

NSString *linkHeader = [(NSHTTPURLResponse *)operation.response  allHeaderFields][@"Link"];
NSArray *links = [linkHeader componentsSeparatedByString:@","];

Then I am doing the following to get all links I needed

RACSequence *sequence = [links.rac_sequence map:^id(NSString* item) {
                NSError *error;
                NSLog(@"item is %@",item);

                NSRegularExpression *regex  =   [NSRegularExpression regularExpressionWithPattern:@"<(.*)>" options:0 error:&error];
                NSString *actualLink;

                if (regex != nil) {
                    NSTextCheckingResult *result    =   [regex firstMatchInString:item
                                                                          options:0
                                                                            range:NSMakeRange(0, item.length)
                                                         ];
                    if (result) {
                        NSRange range    =   [result rangeAtIndex:1];
                        actualLink       =   [item substringWithRange:range];
                        NSLog(@"actualLink is %@",actualLink);

                        return [RACSequence return:actualLink];

                    }
                }
                return [RACSequence empty];

            }];

Right now sequence is containing all links which are

http://thisIsCurrentlink&limit=24
http://thisIsLastlink&limit=24
http://thisIsFirsttlink&limit=24

My question is how can I access each item from above sequence

1

There are 1 answers

2
Charles Maria On BEST ANSWER

Just use the array method, which converts RACSequence into an array.

Also, you don't need to use RACSequence return:, that's creating sequences within your sequence, just return the usual string and nil, then use a filter after the map to remove all nil values before trying to extract it as an array.

Example:

NSArray *actualLinks = [[links.rac_sequence map:^id(NSString* item) {
            NSError *error = nil;
            NSLog(@"item is %@",item);

            NSRegularExpression *regex  =
            [NSRegularExpression regularExpressionWithPattern:@"<(.*)>" options:0 error:&error];
            NSString *actualLink = nil;

            if (regex != nil) {
                NSTextCheckingResult *result = [regex firstMatchInString:item
                                                                        options:0
                                                                        range:NSMakeRange(0, item.length)];
                if (result) {
                    NSRange range = [result rangeAtIndex:1];
                    actualLink = [item substringWithRange:range];
                    NSLog(@"actualLink is %@",actualLink);
                }
            }
            return actualLink;
        }] ignore:nil].array;