1

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 ?

Community
  • 1
  • 1
bguler
  • 237
  • 1
  • 5
  • 11

1 Answers1

2

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
nst
  • 3,862
  • 1
  • 31
  • 40
  • i tried but it still return tweet. can you send example code ? thanks. – bguler Jul 05 '14 at 15:07
  • Thanks @nst it solved problem. I have also this.. http://stackoverflow.com/questions/24632675/get-bounding-box-from-lat-long-and-distance maybe you can help. – bguler Jul 08 '14 at 19:15