IOS HTTP request twice
I have a function that makes a GET request to a server. It works fine, but for some reason it is called twice. I am calling a function when a button is clicked.
This is the function code:
-(void) GETasync: (NSString *) path{
receivedData = [[NSMutableData alloc] init];
NSURLRequest *request=[NSURLRequest requestWithURL:
[NSURL URLWithString: path]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
NSHTTPURLResponse * response;
NSError * error;
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"data received");
}
So, I see in the output:
2012-03-07 16:36:41.509 KW2[24136:bf03] data received
2012-03-07 16:36:41.694 KW2[24136:bf03] data received
I also have a function for POST request and I have the same problem with it.
source to share
I am assuming you are printing this log in your delegate method connection:didReceiveData:
. This method can be called multiple times for one single compound - in fact, it is usually called at least twice.
From the documentation:
The delegate periodically sends connection: didReceiveData: messages when data is received. The delegate implementation is responsible for storing the newly received data.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
EDIT: I see that in your last changes you added that registration information (or maybe I didn't see it right away - shame on me).
Perhaps you are calling this method with a notification handler? If yes, you may have registered twice for this notification -> hence your handler is being called twice.
source to share