如何在iOS5中使用TWO请求performRequest WithHandler同步变量?

时间:2012-08-18 09:26:32

标签: iphone ios5 twitter objective-c-blocks twrequest

我真的无法弄清楚如何处理iOS5 Twitter API TWRequest performRequestWithHandler。我在.h文件中声明了NSMutableArray * parsedTimeLine的实例变量,并且方法签名为 - (void)fetchWebData:(NSString *)screenName;

所述方法实现在这里:

- (void)fetchWebData:(NSString *)screenName {

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:screenName forKey:@"screen_name"];
[params setObject:@"f" forKey:@"include_entities"];
[params setObject:@"f" forKey:@"include_rts"];
[params setObject:@"10" forKey:@"count"];
[params setObject:@"t" forKey:@"trim_user"];
[params setObject:@"f" forKey:@"contributor_details"];
[params setObject:@"t" forKey:@"exclude_replies"];

NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json"];

TWRequest *request = [[TWRequest alloc] initWithURL:url
                                         parameters:params
                                      requestMethod:TWRequestMethodGET];
[request performRequestWithHandler:
 ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

     if (responseData) {
         NSError *jsonError;

         parsedUserTimeLine =[NSJSONSerialization JSONObjectWithData:responseData
                                                         options:NSJSONReadingMutableLeaves
                                                           error:&jsonError];

         NSLog(@"jsonArray result count inside request: %d",parsedUserTimeLine.count);
         NSLog(@"jsonArray values inside request: %@", parsedUserTimeLine);
     }
 }];   
NSLog(@"jsonArray result count outside request: %d",parsedUserTimeLine.count);
NSLog(@"jsonArray values outside request: %@", parsedUserTimeLine);    

}

这很好用,但有一个奇怪的事情,我真的无法弄清楚。我的2个NSLogs INSIDE请求方法显示了parsedUSerTimeline的准确计数值和数组内容。但我的2个NSLogs OUTSIDE THE REQUEST方法显示

-jsonArray结果计数在请求外:0 请求外的-jsonArray值:(null)

另外,在调用fetchWebData方法后,我在viewDidLoad中检查了parsedUserTimeLine的值,显示count = 0和value = null。

请帮助我这些家伙。我需要全局访问parsedUserTimeLine的值。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我不知道Twitter-Framework,但是从你发布的代码我可以告诉你这将是异步执行。 NSLog都有效。请求完成后调用内部NSLog,即使请求尚未启动,也会调用外部。

如果请求已经完成,只需调用一个方法通知其他人。

[request performRequestWithHandler:
 ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

 if (responseData) {
     NSError *jsonError;

     NSDictionary *parsedUserTimeLine =[NSJSONSerialization JSONObjectWithData:responseData
                                                     options:NSJSONReadingMutableLeaves
                                                       error:&jsonError];

     NSLog(@"jsonArray result count inside request: %d",parsedUserTimeLine.count);
     NSLog(@"jsonArray values inside request: %@", parsedUserTimeLine);

     // The request will be perform on another thread, so call the method on the main
     // thread to avoid crossing operations
     [NSOperationQueue mainQueue] addOperationWithBlock:^{
         [self requestHasFinishedWithData:parsedUserTimeLine];
     }]

 }
}];