2011년 11월 1일 화요일

[iOS5] 트위터 파싱 : 백그라운드 처리 몇 줄로 끝내기

정말 간단하다.


#define queue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define url [NSURL URLWithString: @"http://api.twitter.com/1/statuses/public_timeline.json?count=3&include_entities=true"] //2


#import "ViewController.h"


@implementation ViewController


- (void)viewDidLoad
{
  [super viewDidLoad];


  dispatch_async(queue, ^{
    NSData* data = [NSData dataWithContentsOfURL: url];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
  });
}


- (void)fetchedData:(NSData *)responseData {
  NSError* error;
  NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
  NSLog(@"created_at : %@",[[json objectAtIndex:0] objectForKey:@"created_at"]);
}


@end