Search code examples
objective-cios7twittersttwitter

STTwitter - How can i take just 5 record on Stream API


I used STTwitter in my project and I want 5 tweet on some coordinates. There is question like this but i dont understand.

How can I stop a stream when using STTWitter

I tried like this, but its not stop at 5 records and always return tweet.

-(void)getTwitterActivityWithLocation:(CLLocation *)location withSuccessBlock:(void(^)(NSMutableArray *activities))successBlock
{
    STTwitterAPI *twitter = [STTwitterAPI twitterAPIOSWithFirstAccount];
    [twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
        NSString *latRectLeft = [[NSString alloc] initWithFormat:@"%f",location.coordinate.latitude];
        NSMutableArray *data = [[NSMutableArray alloc] init];

        id twitterRequest = [twitter postStatusesFilterUserIDs:nil keywordsToTrack:nil locationBoundingBoxes:@[@"28.9986108",@"41.0377369",@"28.9996108",@"41.0387369"] delimited:@20 stallWarnings:nil progressBlock:^(NSDictionary *tweet) {


            if ([data count] > 4) {
                [twitterRequest cancel];
                successBlock(data);
            }
            else if (([[tweet valueForKey:@"geo"] valueForKey:@"coordinates"] != nil)) {

                if (![tweet isEqual:nil] && [tweet count] > 0)
                {
                    NSLog(@"%@",[tweet valueForKey:@"text"]);
                    [data addObject:tweet];
                }

            }

        } stallWarningBlock:nil
            errorBlock:^(NSError *error) {
                NSLog(@"Error");
        }];


    } errorBlock:^(NSError *error) {
        NSLog(@"%@",[error description]);
    }];


}

If take [twitterRequest cancel]; line to outside of block, its work. But this time i don't have any tweet record.

How can i solve this ?


Solution

  • Use __block id twitterRequest instead of id twitterRequest.

    Example:

    STTwitterAPI *twitter = [STTwitterAPI twitterAPIOSWithFirstAccount];
    
    [twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
    
        NSMutableArray *data = [NSMutableArray array];
    
        __block id twitterRequest = [twitter postStatusesFilterUserIDs:nil
                                                       keywordsToTrack:nil
                                                 locationBoundingBoxes:@[@"28.9986108",@"41.0377369",@"28.9996108",@"41.0387369"]
                                                             delimited:@20
                                                         stallWarnings:nil
                                                         progressBlock:^(NSDictionary *tweet) {
    
                                                             NSLog(@"-- data count: %lu", (unsigned long)[data count]);
    
                                                             if ([data count] > 4) {
                                                                 NSLog(@"-- cancel");
                                                                 [twitterRequest cancel];
                                                             } else if (([[tweet valueForKey:@"geo"] valueForKey:@"coordinates"] != nil)) {
                                                                 if ([tweet count] > 0) {
                                                                     NSLog(@"%@",[tweet valueForKey:@"text"]);
                                                                     [data addObject:tweet];
                                                                 }
    
                                                             }
    
                                                         } stallWarningBlock:nil
                                                            errorBlock:^(NSError *error) {
                                                                NSLog(@"-- error 2: %@", error);
                                                            }];
    
    } errorBlock:^(NSError *error) {
        NSLog(@"-- error 1: %@", error);
    }];
    

    Logs:

    -- data count: 0
    Rabbim sana cok sukur (...)
    -- data count: 1
    +Sevgilin varmı evladım (...)
    -- data count: 2
    @RussoftMe gt or unf *-*
    -- data count: 3
    Essege altin semer vursan essek yine essektir...
    -- data count: 4
    :-) (@ Pizza Hut) http://t.co/SZim78OnsU
    -- data count: 5
    -- cancel