STTwitter - 我怎样才能在Stream API上获得5条记录

时间:2014-07-04 17:17:42

标签: objective-c ios7 twitter sttwitter

我在我的项目中使用了STTwitter,我希望在某些坐标上发送5条推文。有这样的问题,但我不明白。

How can I stop a stream when using STTWitter

我尝试过这样,但它不会停留在5条记录上并且总是返回推文。

-(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]);
    }];


}

如果将[twitterRequest cancel];行带到块之外,则其工作。但是这次我没有任何推文记录。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

使用__block id twitterRequest代替id twitterRequest

示例:

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);
}];

日志:

-- 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