Observe a single CoreData object and update a SwiftUI component

Is this the way to go?

class UserStore: ObservableObject {
  @Published var isPremium: Bool
  
  init(id objectID: NSManagedObjectID,
       in context: NSManagedObjectContext) {
    if let currentUser = try? context.existingObject(with: objectID) as? User {
      self.isPremium = currentUser.isPremium
    } else {
      log.info("No user with \(objectID) exists")
    }
  }
}

This topic was automatically closed after 166 days. New replies are no longer allowed.