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)
}
}
`