title: SWIFT: In App purchase crashes but displays PaymentSheet. After the first crash, it all works fine
body: <p>My ViewDidLoad calls all of the functions for fetchProduct...</p>
  <p>This works fine in Sandbox on device when i build from XCode, but when I build to test flight, the app crashes, but the PaymentSheet is displayed over the home screen upon crashing.
  Each time the user tries to purchase after the crash, it works fine. But the crash is immediately causing loss of purchases.</p>
  <pre><code>  
      func fetchProductLove(){
          print(&quot;Fetching&quot;)
          let request = SKProductsRequest(productIdentifiers: [&quot;LOVEDECK&quot;])
          request.delegate = self
          request.start()
          
      }
      
      func fetchProductExplicit(){
          print(&quot;Fetching&quot;)
          let request = SKProductsRequest(productIdentifiers: [&quot;EXPLICITDECK&quot;])
          request.delegate = self
          request.start()
      }
      
      func fetchProductAll(){
          print(&quot;Fetching&quot;)
          let request = SKProductsRequest(productIdentifiers: [&quot;ALLQUESTIONSDECK&quot;])
          request.delegate = self
          request.start()
      }
      
      func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
          DispatchQueue.main.async {
              if let product = response.products.first { //14:30 of IAP by iOSAcademy for multiple products
                  self.myProduct = product
                  print(product.productIdentifier)
                  print(product.localizedTitle)
                  print(product.localizedDescription)
                  print(product.priceLocale)
                  print(product.price)
                 
              }
              else {
                  print(&quot;Couldnt find product&quot;)
              }
  
          }
          
      }
      
      @IBAction func clickStartGame(_ sender: Any) {
         startLoading()
          if deckIsPurchased == true
          {
              getQuestionsForGame()
          }
          else if deckIsPurchased == false {
            
              if(SKPaymentQueue.canMakePayments()) {
           startTransaction()
              }
              else {
                  let alert  = UIAlertController(title: &quot;Warning&quot;, message: &quot;Apple ID is unable to make purchases. Contact Apple or info@whostmostapp.com to resolve.&quot;, preferredStyle: .alert)
                  alert.addAction(UIAlertAction(title: &quot;Close&quot;, style: .default, handler: nil))
                  self.present(alert, animated: true, completion: nil)
              }
  
          }
      }
      
      
      func startTransaction(){
       
            guard let PurchasingProduct = myProduct else {
             
              print(&quot;Did Not Find At All&quot;)
              if myProduct == nil {
                  let alert  = UIAlertController(title: &quot;Unable to Download&quot;, message: &quot;The purchase has not been made. Check your connection and try again&quot;, preferredStyle: .alert)
                  alert.addAction(UIAlertAction(title: &quot;Close&quot;, style: .default, handler: nil))
                  self.present(alert, animated: true, completion: nil)
                    endLoading()
              }
              
              return
            }
          
          if myProduct != nil{
                let payment = SKPayment(product: PurchasingProduct)
                  print(payment)
                SKPaymentQueue.default().add(self)
                SKPaymentQueue.default().add(payment)
          }
                
      }
      
      func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
          for transaction in transactions {
              print(transaction.error ?? &quot;&quot;)
              switch transaction.transactionState {
      
              
              case .purchasing:
                 
              break
              
              case .purchased, .restored:
                  //unlock item
                  print(&quot;DeckRefName&quot;,deckNameRefValue)
                  if deckNameRefValue == &quot;LovePotion&quot;{
                  UserDefaults.standard.set(&quot;Yes&quot;, forKey: &quot;hasPurchasedLovePotionDeck&quot;)
  
                  }
                  else if deckNameRefValue == &quot;Explicit&quot;{
                  UserDefaults.standard.set(&quot;Yes&quot;, forKey: &quot;hasPurchasedExplicitDeck&quot;)
                  }
                  else if deckNameRefValue == &quot;All&quot;{
                  UserDefaults.standard.set(&quot;Yes&quot;, forKey: &quot;hasPurchasedLovePotionDeck&quot;)
                  UserDefaults.standard.set(&quot;Yes&quot;, forKey: &quot;hasPurchasedExplicitDeck&quot;)
                  UserDefaults.standard.set(&quot;Yes&quot;, forKey: &quot;hasPurchasedAllDeck&quot;)
                  }
                  SKPaymentQueue.default().finishTransaction(transaction)
                  SKPaymentQueue.default().remove(self)
                  layoutForCompletedPurchase()
                  deckIsPurchased = true
                  Analytics.logEvent(AnalyticsEventPurchase, parameters: [&quot;Product&quot;: deckNameRefValue, &quot;Price&quot;:deckPriceValue])
  
                  endLoading()
                  
              break
                  
                  
                  
              case .failed, .deferred:
                  //no operation
                  SKPaymentQueue.default().finishTransaction(transaction)
                  SKPaymentQueue.default().remove(self)
                  let alert  = UIAlertController(title: &quot;Purchase Failed&quot;, message: &quot;Unable to complete the purchase. Check your network connection and try again&quot;, preferredStyle: .alert)
                  alert.addAction(UIAlertAction(title: &quot;OK&quot;, style: .default, handler: nil))
                  self.present(alert, animated: true, completion: nil)
                  endLoading()
              break
              
             default:
                  //no operation
  
              SKPaymentQueue.default().finishTransaction(transaction)
              SKPaymentQueue.default().remove(self)
              
                  break
              }
          }
          
      }
  </code></pre>