Created
November 1, 2013 14:09
-
-
Save delebedev/7265957 to your computer and use it in GitHub Desktop.
Bind RAC to AFNetworking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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