Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle responses that come in multiple waves of data

connection:didReceiveData: may be called multiple times, so parsing JSON
after just one will result in parse errors. Collect the data across
those calls and parse the JSON in connectionDidFinishLoading: instead.

Signed-off-by: Tim Shadel <github@timshadel.com>
  • Loading branch information...
commit c070bfef1f1741f15a5fd96a90e72cc1af7dbc4c 1 parent 1296770
@timshadel timshadel authored committed
View
1  Classes/LROAuth2DemoViewController.h
@@ -14,6 +14,7 @@
@interface LROAuth2DemoViewController : UITableViewController <ASIHTTPRequestDelegate> {
LROAuth2AccessToken *accessToken;
NSArray *friends;
+ NSMutableData *_data;
}
@property (nonatomic, retain) LROAuth2AccessToken *accessToken;
@property (nonatomic, retain) NSArray *friends;
View
11 Classes/LROAuth2DemoViewController.m
@@ -92,6 +92,8 @@ - (void)loadFacebookFriends;
{
NSString *URLString = [NSString stringWithFormat:@"https://graph.facebook.com/me/friends?access_token=%@", [self.accessToken.accessToken stringByEscapingForURLQuery]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
+ [_data release]; _data = nil;
+ _data = [[NSMutableData alloc] init];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[NSURLConnection connectionWithRequest:request delegate:self];
@@ -100,12 +102,15 @@ - (void)loadFacebookFriends;
#pragma mark -
#pragma mark NSURLConnection methods
-- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
-{
+- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
+ [_data appendData:data];
+}
+
+- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSError *jsonError = nil;
- NSDictionary *friendsData = [data yajl_JSON];
+ NSDictionary *friendsData = [_data yajl_JSON];
if (jsonError) {
NSLog(@"JSON parse error: %@", jsonError);
} else {
Please sign in to comment.
Something went wrong with that request. Please try again.