How to synchronize variables with TWRequest performRequestWithHandler in iOS5?

821 views Asked by At

I really couldn't figure out how to deal with iOS5 Twitter API TWRequest performRequestWithHandler. I declared an instance variable of NSMutableArray *parsedTimeLine in my .h file and a method signature of -(void) fetchWebData: (NSString *) screenName;

The said method implementation is here:

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

}

This works fine, but there's a weird thing I really couldn't figure out. My 2 NSLogs INSIDE the request method shows the accurate count value and array contents of the parsedUSerTimeline. But my 2 NSLogs OUTSIDE THE REQUEST method shows

-jsonArray result count outside request: 0 -jsonArray values outside request: (null)

also, I checked the value of parsedUserTimeLine at my viewDidLoad after calling the fetchWebData method shows count = 0 and value = null.

Kindly help me with this guys. I need to access the value of parsedUserTimeLine globally. Thanks in advance.

1

There are 1 answers

1
AudioBubble On BEST ANSWER

I don't know the Twitter-Framework, but from the code you've posted i can tell you that this is going to perform asynchron. The NSLogs are both valid. The inner NSLogs are called when the request has finished, the outer are called even if the request has not been started.

Simply call a method to inform the others if the request has been finished.

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

 }
}];