title: I Have To Restart A Firestore SnapshotListener And I Don't Think That's Right
body: <p><em><strong>UPDATED:</strong></em> I have a project that is using Firebase Firestore. I have snapshot listeners set up to my model objects. I start the snapshot listeners when the app starts. My understanding is that if you start a snapshot listener once that's all you need.</p>
  <p>My issue is that when I create a new object in the app I must restart the snapshot listener in order for it to see the changes. I am wondering if I am making a mistake somewhere. Here are the files I think are important. If anything else is needed please let me know.</p>
  <p><strong>Main</strong>
  Main where I create the environment objects for the project and start the UserRepository snapshot listener.</p>
  <pre><code>@main
  struct TheExchangeApp: App {
      
      // @EnvironmentObjects
      @StateObject private var userRepository = UserRepository()
          @StateObject private var authListener = AuthSession(userRepository: UserRepository())
  
      var body: some Scene {
          WindowGroup {
              ContentView()
                  .environmentObject(userRepository)
                  .environmentObject(authListener)
          }
      }
  }
  </code></pre>
  <p><strong>UserRepository.swift</strong>
  This is the user repository were I create the snapshot listener.</p>
  <pre><code>    class UserRepository: ObservableObject {
      
          // Access to Firestore Database
          let db = Firestore.firestore()
          private var snapshotListener: ListenerRegistraion?
          
          @Published var users = [User]()
          
          init() {
              startSnapshotListener()
          }
          
          func startSnapshotListener() {
              // Add a SnapshotListener to the User Collection.
          if snapshotListner == nil {
              db.collection(&quot;users&quot;).addSnapshotListener { (querySnapshot, error) in
                  if let error = error {
                      print(&quot;Error getting documents: \(error)&quot;)
                  } else {
                      guard let documents = querySnapshot?.documents else {
                          print(&quot;No Users.&quot;)
                          return
                      }
                      
                      self.users = documents.compactMap { user in
                          do {
                              return try user.data(as: User.self)
                          } catch {
                              print(error)
                          }
                          return nil
                      }
                  }
              }
          }
      }
  
  func stopSnapshotListener() {
          if snapshotListener != nil {
              snapshotListener?.remove()
              snapshotListener = nil
          }
      }
      
      func createNewUser(userUid: String, user: User) {
          do {
              let _ = try self.db.collection(FirestoreCollection.users).document(userUid).setData(from: user)
  
         if snapshotListener == nil {
              print(&quot;createNewUser snapshotListener is nil&quot;)
          } else if snapshotListener != nil {
              print(&quot;createNewUser snapshotListener is not nil&quot;)
          }
          } catch let error as NSError {
              print(&quot;UserRepository - createNewUser Error: \(error)&quot;)
          }
      }
  }
  </code></pre>
  <p><strong>AuthSession</strong>
  Here I start a combine publisher on the userRepository snapshot listener. The publisher is not getting an update when I create a new user in SignUpView below. If I restart the snapshot listener in UserRepository I get the published value.</p>
  <pre><code>class AuthSession: ObservableObject {    
      let db = Firestore.firestore()
      
      var userRepository: UserRepository
      
      @Published var currentUser: User?
      
      private var cancellables = Set&lt;AnyCancellable&gt;()
      
      init(userRepository: UserRepository) {
          self.userRepository = userRepository
          self.startCombine()
      }
      
      func startCombine() {
          userRepository
              .$users
              .receive(on: RunLoop.main)
              .map { users in
                  users
                      .first(where: { $0.id == self.currentUserUid})
              }
              .assign(to: \.currentUser, on: self)
              .store(in: &amp;cancellables)
      }
  }
  </code></pre>
  <p><strong>SignUp View</strong>
  Here is where I create a new user. The snapshot listener does not work unless I restart. I know because the combine publisher in AuthSession does not fire.</p>
  <pre><code>        struct SignUpView: View {
                  @EnvironmentObject var authSession: AuthSession
                  
                  let db = Firestore.firestore()
                  
                  var body: some View {
                      
                          Button(action: {
                              self.signUp()
                          }, label: {
                              Text(&quot;Sign Up&quot;)
                          })
                  }
                  
     func signUp() {
          authSession.listen()
          let newUser = User(email: myemail@email.com, name: &quot;Hans&quot;)
          self.authSession.userRepository.createNewUser(userUid: user.uid, user: newUser)
          
      }
   }
  </code></pre>
  <p>Any help here would be greatly appreciated.</p>