複数の非同期処理を順番に実行したい時に便利なライブラリ。
これが
id q1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); id q2 = [NSOperationQueue new]; dispatch_async(q1, ^{ id md5 = md5(email); id url = [@"http://gravatar.com/" stringByAppendingString:md5]; id rq = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [NSURLConnection sendAsynchronousRequest:rq queue:q2 completionHandler:^(id rsp, id data, id err) { UIImage *gravatarImage = [UIImage imageWithData:data]; dispatch_async(dispatch_get_main_queue(), ^{ self.imageView.image = gravatarImage; }); }]; });
こんな感じで書ける
#import "PromiseKit.h" dispatch_promise(^{ return md5(email); }).then(^(NSString *md5){ return [NSURLConnection GET:@"http://gravatar.com/%@", md5]; }).then(^(UIImage *gravatarImage){ self.imageView.image = gravatarImage; });
いい感じ。