title: SKPaymentQueue finishTransaction: within network completion block?
body: <p>This problem has been giving me headaches for a while now.</p>
  
  <p>My app operates on an auto-renewable subscription model. I have an SKPaymentQueue transactionObserver that is notified when a purchase is successfully completed through Apple, and at this point I send the Apple receipt to my server to verify on my end. Once I verify it, I send a success call back to my app which provides the content to the user and subsequently calls finishTransaction: on the transaction (which <a href="http://developer.apple.com/library/mac/#documentation/StoreKit/Reference/SKPaymentQueue_Class/Reference/Reference.html" rel="nofollow">according to the docs</a> is the proper course of actions). </p>
  
  <p>The problem is, I'm afraid waiting this long to call finishTransaction: after Apple expects it to be called (3-4 seconds) is prohibiting the transaction from actually "finishing". If I successfully make a purchase within my app (and finishTransaction: has been called), and then terminate my app ("stop" in Xcode) and run it again, immediately on startup there will be one, two, three, and sometimes four or five transactions in the SKPaymentQueue before the UI has even loaded. </p>
  
  <p>Here is the call to my server which takes place after the transaction is completed with Apple 
  :</p>
  
  <pre><code>-(void)completeTransaction:(SKPaymentTransaction *)transaction
  {
      NSLog(@"complete transaction");
      [self sendReceiptToServer:transaction];
  }
  
  -(void)sendReceiptToServer:(SKPaymentTransaction*)transaction
  {
      NSString *rawReceiptData = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
      NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:rawReceiptData, @"receipt", [[UserSingleton sharedInstance] memberID], @"member", nil];
  
      NSMutableURLRequest *request = [[HTTPRequestSingleton sharedClient] requestWithMethod:@"POST" path:cFunctionAddCredits parameters:params];   
      AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
  
          NSLog(@"%@", JSON);
          NSString *creditsString = [JSON valueForKey:@"limit"];
          int numberOfCredits = [creditsString intValue];
          [self provideContent:transaction.payment.productIdentifier withNumberOfCredits:numberOfCredits];
          [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  
      } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
          NSLog(@"failed to send receipt to server");  
          [[NSNotificationCenter defaultCenter] postNotificationName:cFailedToSendReceiptToServer object:transaction];
          [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  
      }];
  
      [operation start];
  }
  </code></pre>
  
  <p>As you can see, <code>[[SKPaymentQueue defaultQueue] finishTransaction:transaction];</code> gets called within the success or failure blocks of the request to my server. Could this be problematic somehow?</p>
  
  <p>*<strong><em>EDIT</em>*</strong>
  It seems this issue does not occur if I call <code>SKPaymentQueue:finishTransaction:</code> BEFORE sending the receipt to my server for verification, which leads me to believe that calling it within the network request blocks is indeed causing the problem. But this is not the correct way to handle things, so I'm still stuck.</p>
  
  <p>*<strong><em>EDIT 2</em>*</strong>
  Turns out the transactions sometimes still get "revived" when starting up the app, even if I make the <code>finishTransaction:</code> call before sending the receipt to my server. What's going on?</p>