I want to refresh a table list only when I receive push notification in swiftUI

I want to refresh my list using function call when I receive push notification. Earlier when I used observedobject list updating continuously and it looks like appear and disappear the list view continuously, now I am using stateobject , here list is refreshing only 1 time. ..I am very new in swiftui.Please suggest me how to refresh the else part each time when getting notification

MapviewUI:

**  @StateObject var viewModel = SettingsViewModel()
    @StateObject var vm = MapViewModel()
**
                if pushNotificationReceived == false {
                    ForEach(viewModel.users) { users in
                    NavigationLink(destination: ChatSwiftUIView(chatUser: users)) {
                        
                        VStack(alignment: .leading) {
                            
                            Text(users.email)
                                .multilineTextAlignment(.leading)
                        }
                    }
                    .padding(.horizontal)
                    .padding(.top, 8)
                }
                    
                }
                else{
                    
                    ForEach(vm.users) { users in
                    NavigationLink(destination: ChatSwiftUIView(chatUser: users)) {
                        
                        VStack(alignment: .leading) {
                            Text(users.email)
                                .multilineTextAlignment(.leading)
                        }
                        
                    }
                  
                    .padding(.horizontal)
                    .padding(.top, 8)
                }
                   }
```
mapviewModel:

class MapViewModel: ObservableObject {
    
    @Published var users = [ChatUsers]()
func fetchAllUsers(){
        FirebaseManager.shared.fireStore.collection("users")
            .getDocuments { documentSnapshot, error in
                if let error = error {
                    print("Failed to fetch users : ", error)
                    return
                }
                
                documentSnapshot?.documents.forEach({ snapshot in
                    let data = snapshot.data()
                    self.users.append(.init(data: data))
                })

            }
    }
}

settingsViewModel:

@Published var users = [ChatUsers]()
     func fetchAllUsers(){
         FirebaseManager.shared.fireStore.collection("users")
             .getDocuments { documentsSnapshot, error in
                 if let error = error {
                     print("Failed to fetch users: \(error)")
                     return
                 }
                 
                 documentsSnapshot?.documents.forEach({ snapshot in
                     let data = snapshot.data()
                     let user = ChatUsers(data: data)
                     if user.uid != FirebaseManager.shared.auth.currentUser?.uid {
                         self.users.append(.init(data: data))
                     }
                 })
             }
     }

  • 2

    This needs a minimal reproducible example. No one can run this code and there are important missing parts that relate directly to the question

    – 

  • 1

    Please take a tour and read How to Ask for tips how to write a good question.

    – 

Leave a Comment