SwiftUI List View not updating when (updating before) published property data is retrieved from Firestore

I’ve seen many similar questions posted, but none yet seemed to provide a solution to my version of the problem.

Simply put, a ForEach List view is not updating (or more accurately I suspect is updating too quickly) when the data in a published property is updated as a result of a get request to Firebase Firestore following the creation of new data in Firestore. I’m tearing my hair out as another near identical list, also connected to Firestore, works as desired – the only differnce I’m aware of being dependency injection of my database service functions.

Details:

Model

struct VenueCollection: Codable, Identifiable {
    @DocumentID var id: String?
    
    var name: String
    var description: String?
    var venues: [String: Date]  // Where string is venue ID and Date is date added to collection   
}

ViewModel

class AuthViewModel:NSObject, ObservableObject, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {

 @Published var collections: [VenueCollection] = []

 func getUserCollections() {
        guard let userID = Auth.auth().currentUser?.uid else { return }
        
        databaseService.getUserCollections(userID: userID) { [weak self] result in
            DispatchQueue.main.async {
                switch result {
                case .success(let collections):
                    self?.collections = collections
                case .failure(let error):
                    print("Error fetching collections: (error.localizedDescription)")
                }
            }
        }
    }

}

Database Service

 func getUserCollections(userID: String, completion: @escaping (Result<[VenueCollection], Error>) -> Void){
        let userPrivateCollectionsRef = Firestore.firestore().collection("users").document(userID).collection("private_collections")

        userPrivateCollectionsRef.getDocuments { snapshot, error in
            if let error = error {
                print("Error fetching collections: (error.localizedDescription)")
                completion(.failure(error))
                return
            }

            let fetchedCollections: [VenueCollection] = snapshot?.documents.compactMap { document in
                try? document.data(as: VenueCollection.self)
            } ?? []
            completion(.success(fetchedCollections))
        }
    }

View

struct CollectionsListView: View {
    @EnvironmentObject var authViewModel: AuthViewModel
    @Binding var showCreateCollection: Bool
    var body: some View {
        List {
            ForEach (sortedCollections) { collection in
                CollectionsRowView(venueCollection: collection)
                    .background(
                        NavigationLink("", destination: CollectionView(venueCollection: collection))
                            .opacity(0)
                    )
                    .listRowSeparator(.hidden)    
            }
            
            Button {
                showCreateCollection = true
            } label: {
                PrimaryButtonActiveToggle(isActive: true, ButtonText: "Create collection")
                    .padding(.horizontal)
            }
            .listRowSeparator(.hidden)
        }
        .listStyle(PlainListStyle())
        .onAppear(){
                    authViewModel.getUserCollections()
                }
    }

When a collection is created, a function writes the collection to firebase. I then re-call the getUserCollections function(s) and dismsss the create collection view. Printing out data to the console shows that the count of collections is increased. But the list does not update until navigating away and back to the view, presumably triggering the onAppear getUserCollections, which this time, works.

I can’t figure out why, even though there is a delay in getting the data from Firebase, the list doesn’t update with the array does.

I’ve also tried optimistically updating the local array with the new venue, and that didn’t update the list either.

Hope I’ve shared enough/not too much code. Appreciate any help.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật