2 years ago

#71719

test-img

user1960279

how to manage delete API call to remove object from array in swiftui

I got stuck in one problem. I have a view called Message view where I am already observing a view Model named "MessageDetailViewModel". This view model will already have published Provided along with several API's functions which are getting called by Views.

Now in this Message view, there is also 1 more view added which is CommentsSection View. Where we are passing the same ViewModel. This comments sectionView has 2 separate views. IncomingMessageWidget and OutgoingMessageWidget.

struct CommentsSection: View {
    @ObservedObject var viewModel: MessageDetailsViewModel
    
    var body: some View {
        VStack(spacing: 10) {
            
            if viewModel.comments.count == 0 {
                EmptyBubbleWidget()
            } else {
                ForEach(0..<viewModel.comments.count, id: \.self) { index in
                    let comment = viewModel.comments[index]
                    if comment.isMine! {
                        OutgoingCommentWidget(index: index, messageDetailsViewModel: viewModel)
                    } else {
                        IncomingCommentWidget(index: index, messageDetailsViewModel: viewModel)
                    }
                    Spacer()
                        .frame(height: 20)
                }
            }
        }
    }
}

Below is the OutgoingCommentWidget where I have an action to delete my own comment. After that, I am unable to manage the view as it is crashing.

    struct OutgoingCommentWidget: View {
        
        var index = 0
        @ObservedObject var messageDetailsViewModel: MessageDetailsViewModel
        
        var body: some View {
// SomeCode
                bottomView
                    .frame(width: contentWidth - 15, height: 25)

    }


    extension OutgoingCommentWidget {
        
      
        private var bottomView: some View {
            HStack {                
                    **Button(action: {
                        messageDetailsViewModel.deleteAComment(commentId: messageDetailsViewModel.comments[index].id) { response in
                            if response {
                              //  messageDetailsViewModel.comments.remove(at: index)
                                messageDetailsViewModel.getMessageDetails()
                            }
                        }
                    }, label: {
                        Image(systemName: "arrow.up.bin").foregroundColor(.textPrimary)
                    })**
            }
            .padding(.trailing)
        }
    }

This is the function(messageDetailsViewModel.deleteAComment) from the above file, where the delete comment API from MessageDetailViewModel.

I am again fetching the comments in the message post, but to remove this after getting success from API Call. As soon as I got response control immediately goes to CommentsSection for loop and app crashed because the viewModel still has that comment.

Hope I am clear with the explanation.

ios

arrays

swiftui

observable

viewmodel

0 Answers

Your Answer

Accepted video resources