1

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

使用 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];线到块外,它的工作。但是这次我没有任何推文记录。

我该如何解决这个问题?

4

1 回答 1

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
于 2014-07-05T11:23:03.930 回答