使用Swiftui中的列表以及数据删除对象

发布于 2025-02-08 07:15:45 字数 1885 浏览 1 评论 0原文

我正在尝试找出从list中删除项目的方法,swiftui。我尝试使用.ondlete方法,该方法从数组中删除了条目,但是我不确定如何看到项目的元数据(即对象ID或内容)并使用该条件来从后端删除项目。

        List {
            ForEach(userPostsVM.posts) {
                post in
                FeedViewCell(post: post, isUserPostsView: true)
                    .onAppear{
                        userPostsVM.fetchMoreDataIfNeeded(currentItem: post)
                    }
                
            }.onDelete { indexSet in
                /*this remove the item from the list,
                  however once refreshed the view item
                 is reloaded because it was refetched
                 from the server and was never deleted
                 in the server
                 */
                userPostsVM.posts.remove(atOffsets: indexSet)
                userPostsVM.deleteUserPost(at: indexSet)
            }
        }

尝试了以下方法,但是帖子ID永远不会与从列表行删除的对象匹配

func deleteUserPost(at offsets : IndexSet) {
    self.posts.remove(atOffsets: offsets)
    offsets.forEach({ index in
        let deletedPostId = self.posts[index].id
        deletePostfromServer(postId: deletedPostId)
    })
}

func deletePostfromServer(postId : String) {
    let query = PFQuery(className: "demoClass")
    query.whereKey("objectId", equalTo: postId)
    query.whereKey("user", equalTo: AuthViewModel.shared.currentUser!)
    query.getFirstObjectInBackground { object, _ in
        if let object = object {
            print("DEBUG:  got the object with id \(object.objectId)")
            // above does not match with the object id deleted from the list, hence never runs
        }
    }
}

我想要获得的所有是对象详细信息,即objectId所示的项目特别是已删除的行。 帖子数组是已发布的属性。

我认为,通过将数组状态与以前的状态进行比较,并获得当前状态中的内容,最后使用该对象从服务器后端删除。

任何帮助都将受到赞赏。

I am trying to figure out the way to delete an item from the List in SwiftUI. I tried to use the .onDelete method, which removes the entry from the array, however I am unsure about how can I see the metadata of the item (i.e. object id or content) and use that condition to delete the item from the backend.

        List {
            ForEach(userPostsVM.posts) {
                post in
                FeedViewCell(post: post, isUserPostsView: true)
                    .onAppear{
                        userPostsVM.fetchMoreDataIfNeeded(currentItem: post)
                    }
                
            }.onDelete { indexSet in
                /*this remove the item from the list,
                  however once refreshed the view item
                 is reloaded because it was refetched
                 from the server and was never deleted
                 in the server
                 */
                userPostsVM.posts.remove(atOffsets: indexSet)
                userPostsVM.deleteUserPost(at: indexSet)
            }
        }

Tried the following method, however the post id never matches to the object deleted from the list rows

func deleteUserPost(at offsets : IndexSet) {
    self.posts.remove(atOffsets: offsets)
    offsets.forEach({ index in
        let deletedPostId = self.posts[index].id
        deletePostfromServer(postId: deletedPostId)
    })
}

func deletePostfromServer(postId : String) {
    let query = PFQuery(className: "demoClass")
    query.whereKey("objectId", equalTo: postId)
    query.whereKey("user", equalTo: AuthViewModel.shared.currentUser!)
    query.getFirstObjectInBackground { object, _ in
        if let object = object {
            print("DEBUG:  got the object with id \(object.objectId)")
            // above does not match with the object id deleted from the list, hence never runs
        }
    }
}

All I am looking for to get is the object detail i.e. objectId for the item shown in particular row which was/canbe deleted. Posts array is a Published property.

I think one solution can be possible by comparing the array state to previous state and get what is not in the current state and finally, use that object to remove from the server backend.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文