SwiftUI:来自观察到的单例 属性 的状态变量导致错误

SwiftUI: State variable from observed singleton property leads to error

我使用单例访问 RevenueCat 的订阅产品作为应用程序中各种视图的@ObservedObject

import Foundation
import Purchases
import SwiftUI

class SubscriptionManager: ObservableObject {
    static let shared = SubscriptionManager()

    @Published var offerings: Purchases.Offerings? = nil
    
    public func loadOfferings() {
        Purchases.shared.offerings { (offerings, error) in
            self.offerings = offerings
        }
    }
}

然后在视图中

struct MyView: View {
    @ObservedObject var subManager = SubscriptionManager.shared

    // Currently selected package. Select initially the first one
    // A) like this the var doesn't update when the ObservedObject updates the offerings var
    @State private var selectedPackage: Purchases.Package? = SubscriptionManager.shared.offerings?.current?.availablePackages.first

    // B) like this it gives the error "Cannot use instance member 'subManager' within property initializer; property initializers run before 'self' is available"
    @State private var selectedPackage: Purchases.Package? = subManager.offerings?.current?.availablePackages.first

    var body: some View {
        // Paywall UI which displays the packages once the singleton loaded them and. which changes var selectedPackage if user selects a package
        // ...
    }
}

如何根据 @ObservedObject 发布的 属性 正确定义 @State var,它不会给我错误 A 或 B

一种可能的变体是相应地观察设置状态下管理器的变化,例如

struct MyView: View {
    @ObservedObject var subManager = SubscriptionManager.shared

    @State private var selectedPackage: Purchases.Package?

    // ... 

    var body: some View {
      
       SomeViewHere()
          .onChange(of: subManager.offerings) {
             self.selectedPackage = [=10=]?.current?.availablePackages.first  // << here !!
          }

    }
}

您还应该更新主队列中已发布的 属性(或者如果最低规格允许,则让 SubscriptionManager 成为主角),例如

public func loadOfferings() {
    Purchases.shared.offerings { (offerings, error) in
        DispatchQueue.main.async {
           self.offerings = offerings
        }
    }
}