読者です 読者をやめる 読者になる 読者になる

ヤルキデナイズド

Unclassified Articles on Software and IT

AFNetworking-ReactiveCocoa を作った

プログラミング

AFNetworking を ReactiveCocoa に対応させるやつを作った。 Podfile に pod 'AFNetworking-ReactiveCocoa' と書けばインストールできる。使い方はこんな感じ:

#import <AFNetworking/AFNetworking.h>
#import <AFNetworking-ReactiveCocoa/AFNetworking-ReactiveCocoa.h>
#import <ReactiveCocoa/ReactiveCocoa.h>

- (void)requestAndSubscribe {
  AFHTTPClient *client = ...;
  NSURLRequest *request = ...;

  // Enqueue the request and get a signal.
  RACSignal *signal = [client rac_enqueueHTTPRequestOperationWithRequest:request];

  // Subscribe the signal, which will send a tuple of the request operation
  // and the response object.
  [signal
   subscribeNext:^(RACTuple *opAndResp) {
     AFHTTPRequestOperation *operation = [opAndResp first];
     id responseObject = [opAndResp second];
     NSLog(@"success: operation=%@, responseObject=%@", operation, responseObject);
  }
   error:^(NSError *error) {
     // The error object is the same one that is passed to the failure handler
     // of the enqueued request operation, except one additional key-value pair
     // in `userInfo` as shown below.
     AFHTTPRequestOperation *operation = [error.userInfo objectForKey:@"AFHTTPRequestOperation"];
     NSLog(@"error: operation=%@", operation);
   }
   completed:^{
     NSLog(@"completed");
   }];
}

他のメソッドについては AFHTTPClient+RACSupport.h を参照。