title: Multiple POST Requests Occurring
body: <p>I am seeing strange behaviour on an iOS app with a Rails/Heroku backend. I'll try to give as much detail as possible. Hopefully someone can point out a few possible areas where the problem could arise.</p>
  
  <p>I wrote an iOS 7 app using XCode 5 where a user can log in and POST new articles and comments. I am using the AFNetworking library to communicate with my backend server (Rails 4, Heroku &amp; Postgres). I am POSTing and GETing in the JSON format so I make use of AFJSONRequsetOperation to handle communication.</p>
  
  <p>All my POST's use this method:</p>
  
  <pre><code>- (void) Post:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
           :(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure
  
  
  // allocate a reachability object
  Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
  
  
  reach.unreachableBlock = ^(Reachability*reach)
  {
      dispatch_async(dispatch_get_main_queue(), ^{
          UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
          [HUD flashMessage :@"Whoops" :@"No internet connection" :window];
      });
  
      failure(nil, nil, nil, nil);
  };
  
  reach.reachableBlock = ^(Reachability*reach)
  {
  
      // prepare base URL and calculate signature
      // Something like: https://myapp.herokuapp.com
      NSURL *url = [NSURL URLWithString:BasePath];
  
      AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
      [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
      [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
  
      // Generates the POST URL
      // Something like: https://myapp.herokuapp.com/api/v1/articles.json
      NSString *basePath = [self GetPostURL];
  
      NSLog(@"Post %@ With parameters: %@", basePath, Parameters);
  
      NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:basePath parameters:Parameters];
  
      AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:success failure:failure];
  
      [operation start];
  };
  
  
  [reach startNotifier];
  </code></pre>
  
  <p>}</p>
  
  <p>I did a bit of testing on my iPhone 5. Viewing the list of articles in a tableview, viewing article details in a detail view and then another tableview to view and add comments. I did a lot of testing and noticed no problem. Eventually I saw that there was 3 of the exact same comments. I thought I must of double-tapped on the 'Add' button so disable the button on-tap. I thought no-more about it as it was near impossible to re-create.</p>
  
  <p>I have since updated my phone to iOS 8 and updated my XCode and SDKs to reflect this.
  I put the app on my friends phone (iOS 8 &amp; iPhone 5 C). She used it for a day without seeing any problems. She viewed articles, comments and added her own with no problems.</p>
  
  <p>Today was a different story.
  Firstly, a bit of background information. When you open the app - I refresh the list of articles so the app usually appears showing the old list of articles, then a loading icon appears, the tableview flashes and the newest articles appear.</p>
  
  <p>When she opened the app today - it flashed multiple times, it looked like 10 or so. When the tableview appeared, the article she posted yesterday appeared twice. The exact article (title, description ,lat/lng, image) appeared twice but with different timestamps. </p>
  
  <p>I thought the app was playing up on her phone so I checked the backend. It had two separate articles, different ids and timestamps but an exact copy otherwise.</p>
  
  <p>Looking at the logs, there was something like 20/30 GET requests from her account. This seems to correspond with the flashing upon opening. It was updating the tableview multiple times.</p>
  
  <p>Then the logs showed a POST request with all the params she POSTed the day before. Its as if she actually posted the exact same article today - but obviously she didn't.</p>
  
  <p>Has anyone seen this kind of thing before?</p>
  
  <p>The only things I can think of are:</p>
  
  <ul>
  <li>The add article view controller was not destroyed after the initial post and somehow was made active again. I presumed ARC would handle all this?</li>
  <li>Perhaps the AFNetworking library doesn't get flushed and thinks it needs to post again? Albeit nearly 24 hours later.</li>
  <li>Is there a request batch operation on the library? Maybe this could be a problem?</li>
  <li>I use a 'dispatch_get_global_queue' when posting. Is this okay?</li>
  <li>On successfully adding an article, I move to the list again using the below code. Maybe there is a problem with it that I am unaware of?</li>
  </ul>
  
  <p><code>
      ArticleListViewController *articleList = (ArticleListViewController *)[self.storyboard instantiateInitialViewController];
      articleList.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
      [self articleList animated:YES completion:nil];
  </code></p>
  
  <p>I am at a loss trying to even replicate it, not to mention fix it.
  I assume I am correct in pointing the finger at the app? It doesn't seem to be a problem with heroku/rails.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I just had this issue again.
  I did some testing yesterday, adding 3 articles. Everything worked as expected. I would occasionally go into the app every few hours to make sure everything is okay. I just opened the app now and it started flashing/flickering. It updated the list with a new article I posted yesterday. I did nothing with and it flickered again, and the same article was added again (so thats 2 duplicates with todays created_at date). I continued doing nothing and in a few seconds the other 2 articles that I added yesterday had been updated. One article was added 3 times, one article 2 times and one article was just duplicated once. This is bizarre behaviour.</p>
  
  <p>Could there be some sort of global variable or something that keeps all the data? Maybe the controllers aren't removed completely?</p>
  
  <p>Brian</p>