Question

I have an IBAction connected to a UIButton, which when pressed calls an HTTPS POST request method. The method is called and the request is made, the server responds and everything is dandy.

The problem that I am having is very peculiar. I launch the app, and I hit my button. The request is made once. I hit it again, and the request fires twice, I hit it again and the request fires three times, and so on and so forth. I continued up to 20 POST requests made with a single button press!

I set breakpoints to see if the method was being called more than once, and the breakpoint only showed one. But, then I put in a log to log the request, and that showed me that it was repeating, but for some unknown reason.

I have no loops (that I can see), no NSTimers, or any reason for this to be happening. Here is the IBAction connected to the button:

- (IBAction)startDrill:(id)sender
{
    chosenDict = startDict;
    [self submitDrillControl];
    NSLog(@"now");
}

Here is the NSDictionary that it is setting:

    startDict = [NSDictionary dictionaryWithObjectsAndKeys:personID,@"person", selectedDrillID,@"drill", @"start",@"action", nil];

And here is the POST method

- (void)submitDrillControl
{
    NSLog(@"1");

    NSError *error;
    NSData *jsonParams = [NSJSONSerialization dataWithJSONObject:chosenDict options:NSJSONWritingPrettyPrinted error:&error];

    NSString *paramString = [[NSString alloc] initWithData:jsonParams encoding:NSUTF8StringEncoding];

    NSString *bodyData = [NSString stringWithFormat:@"token=%@&method=drill_control&params=%@", savedToken, paramString];

    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://my.url/"]];

    [postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Field"];

    [postRequest setHTTPMethod:@"POST"];
    [postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:   [bodyData length]]];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];

    NSLog(@"wtf? %@", postRequest);
    NSLog(@"Connection: %@", conn);
}

(If anyone is wondering what the dictionary is for, it contains parameters which my API requires to perform query.)

I am unsure as to what other code I should post, or if any other code is needed, as I really do not know why this is occurring. Feel free to let me know if I should add, remove, or change something in this question.

EDIT

So after a little more digging (once I woke up), I found that the above is not the POST request that is being repeated. After this POST request, there is a response from the server which triggers an NSNotification. The selector that the NSNotification observer calls, is the POST request that is being repeated. Setting breakpoints shows that it does get called multiple times. Why are the notifications stacking up? How do I keep it from being called more than once?

Was it helpful?

Solution

If you are using the block based NSNotifications they have to be removed individually. Save the notification in an ivar or property and remove it explicitly.

I have seen where the notification is being set multiple times and not being removed. Then each instance fires; sounds like this is what is happening to you.

In particular removeObserver:nil ... will not remove block based observers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top