I want to show the comment immediately on the screen with comments list

when I post comment I have to fetch the comment list (all product details) again to show the new comment
I use combine , any solution to show the comment immediately in the comments list when post?

class ProductDetailsViewModel: ObservableObject{
    @Published var productDetails: ProductDetails? = nil
    @Published  var commentText: String = ""
    func fetchProductDetails(id: Int){
        isLoading = true
        service.fetchProductDetails(id: id)
            .receive(on: DispatchQueue.main)
            .sink { [weak self] completion in
                guard let self = self else { return }
                self.isLoading = false
                switch completion {
                case .finished:
                    break
            } receiveValue: { [weak self] response in
                self?.productDetails = response.data
            }
            .store(in: &cancellables)
    } 

func postComment(id: Int){
        isLoading = true
        let parameters: [String: Any] = ["comment": commentText]
        service.postRequest(url: Config.baseURL+"item/(id)/comment", parameters: parameters, type: CommentResponse.self)
            .receive(on: DispatchQueue.main)
            .sink { [weak self] completion in
                self?.isLoading = false
                switch completion{
                case .finished:
                    break
            } receiveValue: { [weak self] response in
                if let message = response.message{
                    self?.message = message
                }
                self?.commentText = ""
            }
            .store(in: &cancellables)
  if let comments = product.comments, !comments.isEmpty {
                 VStack(spacing: 10){
                    ForEach(comments, id: .id){ comment in
                        AdCommentCellView(comment: comment)
                    }
                }

1

To make the new comment appear immediately after posting, you don’t need to fetch the entire product details again. You can modify the comments list directly after successfully posting a new comment and update the @Published property to trigger the UI update.

class ProductDetailsViewModel: ObservableObject {
    @Published var productDetails: ProductDetails? = nil
    @Published var commentText: String = ""
    @Published var message: String = ""
    @Published var isLoading: Bool = false
    private var cancellables = Set<AnyCancellable>()

    // Fetch Product Details
    func fetchProductDetails(id: Int) {
        isLoading = true
        service.fetchProductDetails(id: id)
            .receive(on: DispatchQueue.main)
            .sink { [weak self] completion in
                self?.isLoading = false
                switch completion {
                case .finished:
                    break
                case .failure(let error):
                    // Handle error here
                    print(error.localizedDescription)
                }
            } receiveValue: { [weak self] response in
                self?.productDetails = response.data
            }
            .store(in: &cancellables)
    }

    // Post Comment and Update Comments List
    func postComment(id: Int) {
        isLoading = true
        let parameters: [String: Any] = ["comment": commentText]
        service.postRequest(url: Config.baseURL + "item/(id)/comment", parameters: parameters, type: CommentResponse.self)
            .receive(on: DispatchQueue.main)
            .sink { [weak self] completion in
                self?.isLoading = false
                switch completion {
                case .finished:
                    break
                case .failure(let error):
                    // Handle error here
                    print(error.localizedDescription)
                }
            } receiveValue: { [weak self] response in
                guard let self = self else { return }
                if let message = response.message {
                    self.message = message
                }

                // Create a new comment object
                let newComment = Comment(id: UUID().uuidString, text: self.commentText)
                
                // Append the new comment to the product's comments list
                self.productDetails?.comments.append(newComment)

                // Clear the comment text after posting
                self.commentText = ""
            }
            .store(in: &cancellables)
    }
}
`

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