3

I have a function which makes GET request to the server. It works fine but for some reason it's called twice. I call function when a button is pressed.

This is 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 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 it's the same trouble with it.

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84

3 Answers3

0

I am assuming that you are printing out that log within your delegate method connection:didReceiveData:. That method can be called multiple times for one single connection - in fact it is commonly called at least twice.

From the documentation:

The delegate is periodically sent connection:didReceiveData: messages as the 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 latest edits you have added that logging information (or maybe I did not see it right away - shame on me).

Are you possibly calling that method by a notification handler? If so, have you possibly double-registered for that notification -> hence your handler is invoked twice.

Till
  • 27,559
  • 13
  • 88
  • 122
0

The problem was just in that I wired IBOutlets from File'Owner and First Responder to buttons in IB.

After removing wires from File's Owner, the method started to be called only one time.

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84
0

Also make sure to check the IBAction connections for your button in Interface Builder. If you copy and paste buttons in IB, you can end up having 2 or more identical IBAction connections for a button, which would cause the method to be executed twice with just one button click.

BP.
  • 10,033
  • 4
  • 34
  • 53