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.

+3


source to share


3 answers


The problem was that I connected IBOutlets from File'Owner

and First Responder

to buttons in IB.



After removing the wires from File Owner

, the method began to be called only once.

0


source


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.

0


source


Also make sure you check the IBAction connections for your button in the Interface Builder. If you copy and paste buttons in IB, you can have two or more of the same IBAction for the button, which will cause the method to execute twice with one click.

0


source







All Articles