24
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AFHTTPSessionManagerをPromiseKit化するメモ

Last updated at Posted at 2014-06-26

swiftナニソレなのでコードはObjective-Cです。

ほぼ公式AFHTTPRequestOperationによるサンプルのまんま。

GETのPromise版オーバーロードを作る。


@interface AFHTTPSessionManager (PromiseKit)

- (PMKPromise *) GET: (NSString *) URLString parameters: (NSDictionary *) parameters {
  return [PMKPromise new:^(PMKPromiseFulfiller fulfiller, PMKPromiseRejecter rejecter) {
    NSURLSessionTask *task =
      [self GET: URLString
       parameters: parameters
          success:^(NSURLSessionTask *task, id responseObject) {
            fulfiller(PMKManifold(task, responseObject));
      }
          failure:^(NSURLSessionTask *task, NSError *error) {
            rejecter(error);
      }
      ];
    [task resume];
  }];
}

@end

fulfillerが成功時に渡す結果、rejecterが失敗時に渡す結果とかそんな感じっぽい。
PMKManifoldを使うことで、一つ以上のオブジェクトの受け渡しにも対応出来る。

こんな感じで使える。

[manager GET: @"nnaaaa" parameters:nil]
  .then(^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"%@", responseObject);
  });

POSTとかも同様な感じですね。

すげー簡単に使えるのでPromiseKitすばらしいですね。

24
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?