I have read all similar problems, but none of the answers worked for me. So let me explain to you what is my issue: I am using NSURLConnection to send async GET request to my backend side. I use to get the right response sometimes, but most of the time I get the last fetched results from the GET request (so I think it has to be something with URL cache).
This is my code where I am creating and sending the URL request:
NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: URL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:kRequestTimeOut];
[URLRequest setHTTPMethod: @"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
if ([NSURLConnection canHandleRequest:URLRequest]) {
[NSURLConnection sendAsynchronousRequest: URLRequest
queue: queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[queue release];
IPBetaLog(@"RESPONSE FROM %@: %@",URL,[[[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding] autorelease]);
IPBetaLog(@"RESPONSE CODE: %i",[(NSHTTPURLResponse*) response statusCode]);
if ([data length] > 0) {
int responseCode = 0;
if ([response isKindOfClass: [NSHTTPURLResponse class]]) {
responseCode = [(NSHTTPURLResponse*) response statusCode];
IPBetaLog(@"RESPONSE CODE: %i",responseCode);
switch (responseCode) {
case 200:
IPLog(@"Received message 'OPERATION_COMPLETED'");
break;
case 201:
IPLog(@"Received message 'OPERATION_COMPLETED'");
break;
case 202:
IPLog(@"Received message 'OPERATION_COMPLETED'");
break;
default:
return block(NO,nil,[self errorWithResponseCode: responseCode]);
break;
}
}
NSError *jsonError = nil;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:&jsonError];
NSLog(@"Result is %@", result);
if (jsonError != nil) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject: NSLocalizedString(@"JSON error", @"") forKey: NSLocalizedDescriptionKey];
[userInfo setObject: jsonError.localizedFailureReason forKey: NSLocalizedFailureReasonErrorKey];
return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorJSONError userInfo: userInfo]);
} else {
return block(YES,result,nil);
}
}
else if ([data length] == 0 && error == nil) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject: NSLocalizedString(@"Response is empty", @"") forKey: NSLocalizedDescriptionKey];
return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorEmptyReponse userInfo: userInfo]);
}
else if (error != nil && error.code == 1001) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject: NSLocalizedString(@"Request timed out", @"") forKey: NSLocalizedDescriptionKey];
return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorTimedOut userInfo: userInfo]);
}
else if (error != nil) {
return block(NO,nil,error);
}
}];
}
else {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject: NSLocalizedString(@"No Connection", @"Error message displayed when not connected to the Internet.") forKey: NSLocalizedDescriptionKey];
return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorBadRequest userInfo: userInfo]);
}
I am using cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData, tried other enums for cache policies, but the problem still exists. Just to prove myself that the problem is in iOS, I have successfully sent the same request from Android and Rest Console and it worked every time.
From my backend side, the request from iOS only comes sometimes and that is the case when the result data is good. For example: I send a GET request with URL to my server side, and request comes, I make the response and it arrives to the iOS with response data. I send another same GET request to same URL, but there is no request on server-side, and iOS give me the same response data from the previous request. Very rarely I success to reach the server-side. Really don't know why. There are no network issues between the iPhone and server-side.
Please if you have any solutions. Thank you very much for your time on my issue.