title: Are observers permanently retained by Swift singletons?
body: <p>My app supports in-app purchases and auto-renewing subscriptions, so it is important that the <code>SKPaymentTransactionObserver</code> is properly notified whenever the renewal notification is published by the system.</p>
  
  <p>I prefer to keep my StoreKit code separate from the AppDelegate, so I created a singleton for it, with a reference in the AppDelegate.</p>
  
  <pre><code>class AppDelegate: UIResponder, UIApplicationDelegate {
  
      let storeKitManager = DFStoreKitManager.shared
  
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -&gt; Bool {
  
          storeKitManager.register()
  
          //...
  
          return true
      }
  
      func applicationWillTerminate(_ application: UIApplication) {
  
          storeKitManager.deregister()
      }
  
  }
  </code></pre>
  
  <p>I add the shared instance as an observer for the <code>SKPaymentQueue</code>.</p>
  
  <pre><code>class DFStoreKitManager: NSObject {
  
      static let shared = DFStoreKitManager()
      private override init() { //This prevents other classes from using the default '()' initializer for this class.
          super.init()
      }
  
      func register() {
          SKPaymentQueue.default().add(self)
      }
  
      func deregister() {
          SKPaymentQueue.default().remove(self)
      }
  
      //...
  }
  
  extension DFStoreKitManager: SKPaymentTransactionObserver {
  
      func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
          for tx in transactions {
  
              switch tx.transactionState {
              case .purchasing:   print("SKPaymentTransaction purchasing")
              case .deferred:     print("SKPaymentTransaction deferred")
              case .failed:       print("SKPaymentTransaction failed")
  
                  //...
                  queue.finishTransaction(tx)
  
              case .restored:     print("SKPaymentTransaction restored")
  
                  //...
                  queue.finishTransaction(tx)
  
              case .purchased:    print("SKPaymentTransaction purchased")
  
                  //...
                  queue.finishTransaction(tx)
              }
          }
      }
  
      func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
          print("Removed \(transactions.count) StoreKit transactions")
      }
  
      func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
          print("SKPaymentTransaction failed to restore through SKPaymentQueue: \(error.localizedDescription)")
      }
  }
  </code></pre>
  
  <p>My logs and analytics show that renewals are consistently being handled through the observer queue, but it seems like the number is significantly lower than the actual number of renewals shown in iTunes Connect.</p>
  
  <p>Starting with iOS 9, NSNotificationCenter will automatically stop sending notifications to observers that have been deallocated (<a href="https://useyourloaf.com/blog/unregistering-nsnotificationcenter-observers-in-ios-9/" rel="nofollow noreferrer">source</a>).</p>
  
  <p>So I'm wondering, <strong>is it possible that my singleton is being deallocated by the system</strong>, so that the <code>SKPaymentTransactionObersver</code> is also removed?</p>
  
  <p>Or is this singleton (and it's observer status) <strong>guaranteed to be retained until the application terminates?</strong></p>