我以遞歸方式調用POST Json web服務來連續上傳/下載數據到服務,直到完成所有操作。可能有多達500-1000個請求。但使用一段時間後,我不斷收到錯誤。 -1012是最常見的。在遞歸POST調用之後繼續「操作無法完成」AFNetworking
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7ae056b0 {NSErrorFailingURLKey=https://api.XXX.com/XXX/XXX/, NSErrorFailingURLStringKey=https://api.XXX.com/XXX/XXX/}
Error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x7ba8e5b0 {NSErrorFailingURLStringKey=, _kCFStreamErrorCodeKey=57, NSErrorFailingURLKey=, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=1, NSUnderlyingError=0x7a6957e0 "The network connection was lost."}
我後下它認爲這隻會發生在iOS8上提到,但我得到兩個iOS7和iOS8上,並在兩個設備/模擬器和WiFi /蘭
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
下面是我是多麼使用方法
-(void) callService {
//counter list variable is already initialized earlier
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:
[NSURL URLWithString:@"https://url.com/url"]];
NSDictionary *params = @{@"id": counter,
@"data": [list objectAtIndex:counter]};
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:@"http://url.com/url/url1/" parameters:params];
AFJSONResponseSerializer *operation = [[AFJSONResponseSerializer
alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation
*operation, id responseObject) {
//Process Data
counter++;
[self callService];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
counter++;
if(error.code == -1012 || error.code == -1005) {
[self performSelector:@selector(callService) withObject:nil
afterDelay:5];
} else {
[self callService];
}
}];
[operation start];
}
即使是連接緊密,從服務器端在這種情況下,確實幫助。
我認爲請求之間應該有一定的差距,否則服務器將會關閉。 –
如果是這樣的話應該是什麼間隔? –
我不知道......有人可能會對此有所幫助。 –