Skip to content

Instantly share code, notes, and snippets.

@delebedev
Created November 1, 2013 14:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delebedev/7265957 to your computer and use it in GitHub Desktop.
Save delebedev/7265957 to your computer and use it in GitHub Desktop.
Bind RAC to AFNetworking
- (RACSignal *)enqueueRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters cacheTime:(NSTimeInterval)expirationTime {
NSAssert(self.cluster, @"cluster should be set before request.");
NSAssert(self.applicationID.length != 0, @"applicationID should be set before request.");
NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:parameters];
RACSignal *signal = [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
AFHTTPRequestOperation *operation;
operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *op, id JSON) {
NSDictionary *errorDictionary = JSON[@"error"];
if (errorDictionary){
NSError *error = [NSError errorWithDomain:WGNClientErrorDomain
code:[errorDictionary[@"code"] integerValue]
userInfo:@{NSLocalizedDescriptionKey:errorDictionary[@"message"]}];
[subscriber sendError:error];
}else{
[subscriber sendNext:JSON];
[subscriber sendCompleted];
}
} failure:^(AFHTTPRequestOperation *op, NSError *error) {
if (![op isCancelled]) {
[subscriber sendError:error];
}
}];
[self enqueueHTTPRequestOperation:operation];
return [RACDisposable disposableWithBlock:^{
[operation cancel];
}];
}];
return [[signal replayLazily] setNameWithFormat:@"-enqueueRequest:%@", request];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment