转载自: http://blog.csdn.net/lgm252008/article/details/50736950
在RAC实际使用时发现RAC有一个在网络请求上很严重的问题,就是同一个网络url刷新时所创建的信号源
内部请求会被执行两次或多次的现象,通过代理Charles很容易看到,查了相关资料有说过该问题但大多数并没有直接指出应怎么处理,
现将
AFHTTPRequestOperationManager和 AFHTTPSessionManager相关代码如下(得修改原码)
- (RACSignal *)rac_requestPath:(NSString *)path parameters:(id)parameters method:(NSString *)method {
return [RACSignal createSignal:^(id subscriber) {
NSURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:nil failure:nil];
RACSignal *signal = [operation rac_overrideHTTPCompletionBlock];
[self.operationQueue addOperation:operation];
[signal subscribe:subscriber];
return [RACDisposable disposableWithBlock:^{
[operation cancel];
}];
}];
}
将后面加上 replay 修改为即可解决:
- (RACSignal *)rac_requestPath:(NSString *)path parameters:(id)parameters method:(NSString *)method {
return [[RACSignal createSignal:^(id subscriber) {
NSURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:nil failure:nil];
RACSignal *signal = [operation rac_overrideHTTPCompletionBlock];
[self.operationQueue addOperation:operation];
[signal subscribe:subscriber];
return [RACDisposable disposableWithBlock:^{
[operation cancel];
}];
}] replay];
}