As a part of offline ID-Objective-C activities and continuation of ID-Objective-Conference 2012 on last February, a meetup was held in Bandung. Held in DyCode office (DyPlex), unfortunately that rare event was only attended by 5 participants. I don’t know that’s because the event was on Saturday (when people might be preparing Saturday night or some may have class as they’re still students), or the iOS platform is still not really interesting for most developers. Or may be nowadays developers are too lazy to learn the most scarce knowledge in Indonesia, knowledge of iOS development. I don’t know. Although there’re many claimed that they’re dying to know about iOS development, yet only 5 persons came 🙂
Anyway, the meetup still went on. We talked about the Storyboard feature in iOS 5 and latest Xcode. I did live coding to create a sample Twitter app that consume tweets stream from Twitter. To consume JSON-based tweets and asynchronously downloading user avatar, I used Grand Central Dispatch and blocks.
Here are most important part of the sample code. This code is for asynchronously downloading tweets stream and parse JSON response.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
- (void)loadTweets { [self.activityView startAnimating]; NSString *_urlString = @"http://twitter.com/status/user_timeline/andri_yadi?count=50"; NSURL *_url = [[NSURL alloc] initWithString:_urlString]; NSMutableURLRequest *_request = [[NSMutableURLRequest alloc] initWithURL:_url]; [_url release]; //Set HTTP headers, to request response in JSON NSDictionary *_headers = [NSDictionary dictionaryWithObjectsAndKeys:@"application/json", @"Accept", nil]; [_request setAllHTTPHeaderFields:_headers]; [_request setTimeoutInterval:60]; [NSURLConnection sendAsynchronousRequest:_request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { //This block of code will be executed when HTTP response is ready or there's an error if (error) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView *_av = [[UIAlertView alloc] initWithTitle:@"There's Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [_av show]; [_av release]; }); } else { /* //For logging response NSString *_responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"response string: %@", _responseString); [_responseString release];*/ NSError *_errorJson = nil; //Parse JSON using built-in JSON parser in iOS 5 self.tweets = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&_errorJson]; dispatch_async(dispatch_get_main_queue(), ^{ self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; [self.tableView reloadData]; [self.activityView stopAnimating]; }); } }]; [_request release]; } |
And this one is for asynchronously downloading user avatar/profile image and displaying it to table cell image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
- (void)downloadImageForIndexPath:(NSIndexPath*)indexPath { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_queue_t mainQueue = dispatch_get_main_queue(); NSDictionary *_tweet = [self.tweets objectAtIndex:indexPath.row]; NSString *_imgUrlString = [_tweet valueForKeyPath:@"user.profile_image_url"]; dispatch_async(queue, ^{ NSData *_imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_imgUrlString]]; UIImage *_img = [UIImage imageWithData:_imgData]; if (_img != nil) { dispatch_async(mainQueue, ^{ UITableViewCell *_cell = [self.tableView cellForRowAtIndexPath:indexPath]; _cell.imageView.image = _img; dispatch_release(queue); }); } }); } |
The complete project is here: TwitterApp
That’s it. See you in another meetups.
maybe its just the exclusive nature of mac development. just getting the logistics to start isn’t cheap. but kudos for you to still spending your weekend to contribute to the community. well done
I used your code, but when i run my app, it doesn’t display images on TableView, but ya, after scrolling tableView, it display images, so can you please give me the solution for it.