From 537bf8313087e8574259c22c2a6ab145bd0cda72 Mon Sep 17 00:00:00 2001 From: Alex Gaspar Date: Thu, 27 Jun 2013 01:24:36 +0200 Subject: [PATCH 1/3] Twitter API : use lookup instead of show to reduce the number of request --- Demo.xcodeproj/project.pbxproj | 4 +- IDTwitterAccountChooserViewController.m | 101 ++++++++++++------------ 2 files changed, 54 insertions(+), 51 deletions(-) diff --git a/Demo.xcodeproj/project.pbxproj b/Demo.xcodeproj/project.pbxproj index 3e08ca6..3b999b9 100644 --- a/Demo.xcodeproj/project.pbxproj +++ b/Demo.xcodeproj/project.pbxproj @@ -93,8 +93,8 @@ isa = PBXGroup; children = ( 763CE32F1677F555006B7525 /* IDTwitterAccountChooserViewController.h */, - 763CE3301677F555006B7525 /* IDTwitterAccountChooserViewController.m */, 763CE3191677F469006B7525 /* AppDelegate.h */, + 763CE3301677F555006B7525 /* IDTwitterAccountChooserViewController.m */, 763CE31A1677F469006B7525 /* AppDelegate.m */, 763CE32B1677F4A7006B7525 /* RootViewController.h */, 763CE32C1677F4A7006B7525 /* RootViewController.m */, @@ -259,6 +259,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch"; INFOPLIST_FILE = "Demo/Demo-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 6.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -270,6 +271,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch"; INFOPLIST_FILE = "Demo/Demo-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 6.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; diff --git a/IDTwitterAccountChooserViewController.m b/IDTwitterAccountChooserViewController.m index 4660a8f..16fbe42 100644 --- a/IDTwitterAccountChooserViewController.m +++ b/IDTwitterAccountChooserViewController.m @@ -82,19 +82,31 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfa - (void)loadMetadataForCellsWithIndexPaths:(NSArray *)indexPaths { - for(NSIndexPath *indexPath in indexPaths) { - ACAccount *account = [self.twitterAccounts objectAtIndex:indexPath.row]; + NSMutableArray *usernameArray = [[NSMutableArray alloc] init]; + + // concat username + for(NSIndexPath *indexPath in indexPaths) { + ACAccount *account = [self.twitterAccounts objectAtIndex:indexPath.row]; NSString *username = [account username]; - if(username == nil || ([imagesDictionary objectForKey:username] != nil && [realNamesDictionary objectForKey:username] != nil)) + if(username == nil || ([imagesDictionary objectForKey:username] != nil && [realNamesDictionary objectForKey:username] != nil)) continue; - NSURL *url = [NSURL URLWithString:[kTwitterAPIRootURL stringByAppendingString:@"users/show.json"]]; - NSDictionary *parameters = @{ - @"screen_name": username - }; - TWRequest *request = [[TWRequest alloc] initWithURL:url + + [usernameArray addObject:username]; + } + + NSString *usernameJoined = [usernameArray componentsJoinedByString:@","]; + NSURL *url = [NSURL URLWithString:[kTwitterAPIRootURL stringByAppendingString:@"users/lookup.json"]]; + + NSDictionary *parameters = @{ + @"screen_name": usernameJoined + }; + + TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET]; - [request setAccount:account]; + // Sign the request with the last account + ACAccount *account = [self.twitterAccounts lastObject]; + [request setAccount:account]; [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if(error != nil && responseData == nil) { NSLog(@"TWRequest error: %@", [error localizedDescription]); @@ -106,48 +118,33 @@ - (void)loadMetadataForCellsWithIndexPaths:(NSArray *)indexPaths { NSLog(@"JSON deserialization error: %@", [error localizedDescription]); return; } - NSString *name = [responseDictionary objectForKey:@"name"]; - if([realNamesDictionary objectForKey:username] == nil && name != nil) { - dispatch_async(dispatch_get_main_queue(), ^{ - [realNamesDictionary setObject:name forKey:username]; - [self.tableView reloadData]; - }); - } - NSString *profileImageURLString = [responseDictionary objectForKey:@"profile_image_url"]; - if([imagesDictionary objectForKey:username] == nil && profileImageURLString != nil) { - NSString *filename = [[profileImageURLString componentsSeparatedByString:@"/"] lastObject]; - NSString *extension = [filename pathExtension]; - NSString *basename = [filename stringByDeletingPathExtension]; - NSString *biggerFilename = [[basename stringByReplacingOccurrencesOfString:@"normal" withString:@"bigger" options:(NSAnchoredSearch | NSBackwardsSearch) range:NSMakeRange(0, [basename length])] stringByAppendingPathExtension:extension]; - NSString *biggerFilenameURLString = [profileImageURLString stringByReplacingOccurrencesOfString:filename - withString:biggerFilename - options:(NSAnchoredSearch | NSBackwardsSearch) - range:NSMakeRange(0, [profileImageURLString length])]; - NSURL *imageURL = [NSURL URLWithString:biggerFilenameURLString]; - TWRequest *imageRequest = [[TWRequest alloc] initWithURL:imageURL - parameters:nil - requestMethod:TWRequestMethodGET]; - [imageRequest setAccount:account]; - [imageRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { - if(error != nil && responseData == nil) { - NSLog(@"TWRequest error: %@", [error localizedDescription]); - return; - } - UIImage *biggerImage = [UIImage imageWithData:responseData]; - UIImage *image = [UIImage imageWithCGImage:[biggerImage CGImage] - scale:2.0 - orientation:UIImageOrientationUp]; - if([imagesDictionary objectForKey:username] == nil) { - dispatch_async(dispatch_get_main_queue(), ^{ - [imagesDictionary setObject:image forKey:username]; - [self.tableView reloadData]; - }); - } - }]; - - } + for (NSDictionary *user in responseDictionary) { + // NSString *name = [user objectForKey:@"name"]; + NSString *name = [user objectForKey:@"name"]; + NSString *username = [[user objectForKey:@"screen_name"] lowercaseString]; + if([realNamesDictionary objectForKey:username] == nil && name != nil) { + dispatch_async(dispatch_get_main_queue(), ^{ + [realNamesDictionary setObject:name forKey:username]; + NSLog(@"--dict---%@",realNamesDictionary); + [self.tableView reloadData]; + }); + } + + NSString *profileImageURLString = [user objectForKey:@"profile_image_url"]; + if([imagesDictionary objectForKey:username] == nil && profileImageURLString != nil) { + dispatch_async(dispatch_get_main_queue(), ^{ + UIImage* myImage = [UIImage imageWithData: + [NSData dataWithContentsOfURL: + [NSURL URLWithString: profileImageURLString]]]; + [imagesDictionary setObject:myImage forKey:username]; + [self.tableView reloadData]; + + + }); + } + } }]; - } + } @@ -203,6 +200,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N // if(properties != nil) { // NSLog(@"properties: %@", properties); // } + NSString *username = [account username]; cell.textLabel.text = [account accountDescription]; @@ -211,6 +209,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N UIImage *image = [imagesDictionary objectForKey:username]; [cell.imageView setImage:image]; + CGFloat widthScale = 34 / image.size.width; + CGFloat heightScale = 34 / image.size.height; + cell.imageView.transform = CGAffineTransformMakeScale(widthScale, heightScale); return cell; } From f0618805acd25ef4382f4cee1bbee343daa9686f Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 27 Jun 2013 01:46:39 +0200 Subject: [PATCH 2/3] Twitter API : use lookup instead of show to reduce the number of request --- IDTwitterAccountChooserViewController.m | 1 - 1 file changed, 1 deletion(-) diff --git a/IDTwitterAccountChooserViewController.m b/IDTwitterAccountChooserViewController.m index 16fbe42..7f26a1f 100644 --- a/IDTwitterAccountChooserViewController.m +++ b/IDTwitterAccountChooserViewController.m @@ -125,7 +125,6 @@ - (void)loadMetadataForCellsWithIndexPaths:(NSArray *)indexPaths { if([realNamesDictionary objectForKey:username] == nil && name != nil) { dispatch_async(dispatch_get_main_queue(), ^{ [realNamesDictionary setObject:name forKey:username]; - NSLog(@"--dict---%@",realNamesDictionary); [self.tableView reloadData]; }); } From f3e4ff8f93022ec4c1f6063ef98dda022d7f175a Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 27 Jun 2013 02:08:46 +0200 Subject: [PATCH 3/3] Twitter API : use lookup instead of show to reduce the number of request --- IDTwitterAccountChooserViewController.m | 78 ++++++++++++------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/IDTwitterAccountChooserViewController.m b/IDTwitterAccountChooserViewController.m index 7f26a1f..f8fa0d7 100644 --- a/IDTwitterAccountChooserViewController.m +++ b/IDTwitterAccountChooserViewController.m @@ -83,43 +83,43 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfa - (void)loadMetadataForCellsWithIndexPaths:(NSArray *)indexPaths { NSMutableArray *usernameArray = [[NSMutableArray alloc] init]; + ACAccount *account; - // concat username for(NSIndexPath *indexPath in indexPaths) { - ACAccount *account = [self.twitterAccounts objectAtIndex:indexPath.row]; - NSString *username = [account username]; + ACAccount *tmpAccount = [self.twitterAccounts objectAtIndex:indexPath.row]; + NSString *username = [tmpAccount username]; if(username == nil || ([imagesDictionary objectForKey:username] != nil && [realNamesDictionary objectForKey:username] != nil)) continue; - [usernameArray addObject:username]; + [usernameArray addObject:username]; + account = tmpAccount; } - NSString *usernameJoined = [usernameArray componentsJoinedByString:@","]; - NSURL *url = [NSURL URLWithString:[kTwitterAPIRootURL stringByAppendingString:@"users/lookup.json"]]; - - NSDictionary *parameters = @{ - @"screen_name": usernameJoined - }; + if([usernameArray count]) { + NSString *usernameJoined = [usernameArray componentsJoinedByString:@","]; + NSURL *url = [NSURL URLWithString:[kTwitterAPIRootURL stringByAppendingString:@"users/lookup.json"]]; + + NSDictionary *parameters = @{ + @"screen_name": usernameJoined + }; + + TWRequest *request = [[TWRequest alloc] initWithURL:url + parameters:parameters + requestMethod:TWRequestMethodGET]; - TWRequest *request = [[TWRequest alloc] initWithURL:url - parameters:parameters - requestMethod:TWRequestMethodGET]; - // Sign the request with the last account - ACAccount *account = [self.twitterAccounts lastObject]; - [request setAccount:account]; - [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { - if(error != nil && responseData == nil) { - NSLog(@"TWRequest error: %@", [error localizedDescription]); - return; - } - error = nil; - NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; - if(error != nil) { - NSLog(@"JSON deserialization error: %@", [error localizedDescription]); - return; - } + [request setAccount:account]; + [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { + if(error != nil && responseData == nil) { + NSLog(@"TWRequest error: %@", [error localizedDescription]); + return; + } + error = nil; + NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; + if(error != nil) { + NSLog(@"JSON deserialization error: %@", [error localizedDescription]); + return; + } for (NSDictionary *user in responseDictionary) { - // NSString *name = [user objectForKey:@"name"]; NSString *name = [user objectForKey:@"name"]; NSString *username = [[user objectForKey:@"screen_name"] lowercaseString]; if([realNamesDictionary objectForKey:username] == nil && name != nil) { @@ -137,16 +137,16 @@ - (void)loadMetadataForCellsWithIndexPaths:(NSArray *)indexPaths { [NSURL URLWithString: profileImageURLString]]]; [imagesDictionary setObject:myImage forKey:username]; [self.tableView reloadData]; - - }); } - } - }]; - + } + }]; + } } + + #pragma mark - Button Actions @@ -195,23 +195,23 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N cell.accessoryType = UITableViewCellAccessoryNone; } ACAccount *account = [self.twitterAccounts objectAtIndex:indexPath.row]; -// NSDictionary *properties = [account valueForKey:@"properties"]; -// if(properties != nil) { -// NSLog(@"properties: %@", properties); -// } + // NSDictionary *properties = [account valueForKey:@"properties"]; + // if(properties != nil) { + // NSLog(@"properties: %@", properties); + // } NSString *username = [account username]; cell.textLabel.text = [account accountDescription]; NSString *realName = [realNamesDictionary objectForKey:username]; cell.detailTextLabel.text = realName; - + UIImage *image = [imagesDictionary objectForKey:username]; [cell.imageView setImage:image]; CGFloat widthScale = 34 / image.size.width; CGFloat heightScale = 34 / image.size.height; cell.imageView.transform = CGAffineTransformMakeScale(widthScale, heightScale); - + return cell; }