You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firestore加载数据后列表删除项无动画问题求助

SwiftUI列表删除无平滑动画的解决办法

我是SwiftUI初学者,正在开发一款从Firestore获取数据并展示在列表中的App。用户可删除列表项,删除时我会从Firestore用户文档的数组中移除对应ID。功能正常,但删除时无动画过渡,视觉体验不佳,希望修改代码实现平滑删除动画。

原代码

import SwiftUI
import FirebaseFirestore
import FirebaseAuth

struct SendToPatientsView: View {
    @Environment(\.dismiss) var dismiss
    @StateObject private var viewModel = AppViewModel()
    
    var body: some View {
        NavigationStack{
            List{
                ForEach(viewModel.userPatients) { patient in
                    NavigationLink{
                        PatientView(results: .constant(patient.patientUID))
                    } label: {
                        Text(patient.patientFamilyName + " " + patient.patientGivenName)
                    }
                    .swipeActions {
                        Button{
                            Task{
                                    do {
                                        try await removeFromPatients(patientUID: patient.patientUID)
                                        
                                    } catch {
                                        print(error.localizedDescription)
                                    }
  
                            }
                        } label: {
                            Image(systemName: "trash.fill")
                        }
                    }
                }
            }
            .task {
                await viewModel.fetchUserData()
                try! await viewModel.getMyPatients()
            }
        }
    }
    
    
    func removeFromPatients(patientUID: String) async throws{
        guard let userID = Auth.auth().currentUser?.uid else {return}
        try await Firestore.firestore().collection("Users").document(userID).updateData(["patients": FieldValue.arrayRemove([patientUID])])
        await viewModel.fetchUserData()
        try await viewModel.getMyPatients()
    }
}

代码更新

之前忘记展示fetchUserData()和getMyPatients()方法,代码如下:

func getAddedPatients() async throws -> [Patient]{
        let viewModel = AppViewModel()
        await viewModel.fetchUserData()

        var userPatient: [Patient] = []
        
        if let myProfile = viewModel.myProfile {
            for id in myProfile.patients {
                guard let patient = try? await Firestore.firestore().collection("Patients").document(id).getDocument(as: Patient.self) else {return []}
                userPatient.append(patient)
            }
        }
        
        return userPatient
    }


func fetchUserData() async{
        guard let userUID = Auth.auth().currentUser?.uid else{return}
        guard let user = try? await Firestore.firestore().collection("Users").document(userUID).getDocument(as: User.self) else {return}
        await MainActor.run(body: {
            myProfile = user
        })
    }

已找到的解决方案

使用.onDelete替代.swipeAction,先本地删除再同步至Firestore,效果良好。

func delete(at offsets: IndexSet) {
        let indices = Array(offsets)
        for index in indices {
            let patient = viewModel.userPatients[index]
            Task {
                do {
                    try await removeFromPatients(patientUID: patient.patientUID)
                } catch {
                    print(error.localizedDescription)
                }
            }
        }
    }

内容的提问来源于stack exchange,提问作者torfeuzarre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 17:15:32