Ios Firebase获取重复数据
每次使用方法:add()时,我都希望使用侦听器将数据保存到Firebase db中。侦听器在开始时获得正确的数据,但当我向Firebase db添加某个内容时,它将再次加载所有数据+旧数据Ios Firebase获取重复数据,ios,swift,firebase,google-cloud-firestore,Ios,Swift,Firebase,Google Cloud Firestore,每次使用方法:add()时,我都希望使用侦听器将数据保存到Firebase db中。侦听器在开始时获得正确的数据,但当我向Firebase db添加某个内容时,它将再次加载所有数据+旧数据 init(appointments: [AppointmentModel] = []) { self.appointments = appointments } var db = Firestore.firestore() func fetch (userId
init(appointments: [AppointmentModel] = []) {
self.appointments = appointments
}
var db = Firestore.firestore()
func fetch (userId: String) {
db.collection("users")
.document(userId)
.collection("appointments")
.addSnapshotListener { (querySnapshot, err) in
if let err = err {
print("Error getting docs: \(err)")
} else {
for document in querySnapshot!.documents {
let d = document.data()
guard let name = d["name"] as? String else {
print("error name")
return
}
guard let time = d["time"] as? String else {
print("error time")
return
}
let appointment = AppointmentModel(
id: document.documentID,
name: name,
time: time,
)
self.appointments.append(appointment)
}
}
}
}
func add(userId: String, name : String, description : String, symbol : String, time : String) {
let identifier = "APPOINTMENT_\(UUID())"
db.collection("users").document(userId).collection("appointments").document(identifier).setData([
"name" : name,
"time" : time
]) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
}
}
}
而不是调用
querySnapshot!。文档
,调用querySnapshot!。文档更改
。这将返回文档更改列表(无论是.added
、.modified
还是.removed
),并允许您根据需要在本地数组中添加、删除或修改这些更改
此外,您可以调用addDocument
,而不是创建自己的UUID
我已经为您重构了代码,处理.added、.modified和.remove:
init(约会:[AppointmentModel]=[]){
自我约会=约会
}
让firestore=firestore.firestore()
func fetch(uid:String){
firestore.collection(“用户/\(uid)/约会”).addSnapshotListener{snapshot,出现错误
guard error==nil,let snapshot=snapshot?.documentChanges else{return}
snapshot.forEach{
let appointment=$0.5
让appointmentId=appointment.documentID
开关$0.0类型{
案例.补充:
guard let name=snapshot.get(“name”)作为?字符串,let time=snapshot.get(“time”)作为?字符串else{break}
self.appoints.append(任命模型(
id:appointmentId,
姓名:姓名,,
时间:时间
))
案例.修改:
guard let localAppointment=(self.appointments.first{$0.id==appointmentId})else{break}
UpdatePoint(本地约会)
案例。删除:
self.appointments=self.appointments.filter{$0.id!=appointmentId}
@未知默认值:
打破
}
}
}
}
@可丢弃结果
func updateapoint(uo约会:约会模型,快照:DocumentSnapshot)->约会模型{
appointment.name=snapshot.get(“name”)为?字符串??appointment.name
appointment.time=snapshot.get(“时间”)为?字符串??appointment.time
回程预约
}
func add(uid:String,name:String,description:String,symbol:String,time:String){
firestore.collection(“users/\(uid)/约会”).addDocument(数据:[“name”:name,“time”:time]){中出现错误
如果let error=error{
打印(“写入文档时出错:\(错误)”)
}否则{
打印(“文档成功写入!”)
}
}
}
不要调用querySnapshot!.documents
,而是调用querySnapshot!.documentChanges
。这将返回文档更改列表(添加的、、修改的、或。删除的),并允许您根据需要在本地数组中添加、删除或修改它们
此外,您可以调用addDocument
,而不是创建自己的UUID
我已经为您重构了代码,处理.added、.modified和.remove:
init(约会:[AppointmentModel]=[]){
自我约会=约会
}
让firestore=firestore.firestore()
func fetch(uid:String){
firestore.collection(“用户/\(uid)/约会”).addSnapshotListener{snapshot,出现错误
guard error==nil,let snapshot=snapshot?.documentChanges else{return}
snapshot.forEach{
let appointment=$0.5
让appointmentId=appointment.documentID
开关$0.0类型{
案例.补充:
guard let name=snapshot.get(“name”)作为?字符串,let time=snapshot.get(“time”)作为?字符串else{break}
self.appoints.append(任命模型(
id:appointmentId,
姓名:姓名,,
时间:时间
))
案例.修改:
guard let localAppointment=(self.appointments.first{$0.id==appointmentId})else{break}
UpdatePoint(本地约会)
案例。删除:
self.appointments=self.appointments.filter{$0.id!=appointmentId}
@未知默认值:
打破
}
}
}
}
@可丢弃结果
func updateapoint(uo约会:约会模型,快照:DocumentSnapshot)->约会模型{
appointment.name=snapshot.get(“name”)为?字符串??appointment.name
appointment.time=snapshot.get(“时间”)为?字符串??appointment.time
回程预约
}
func add(uid:String,name:String,description:String,symbol:String,time:String){
firestore.collection(“users/\(uid)/约会”).addDocument(数据:[“name”:name,“time”:time]){中出现错误
如果let error=error{
打印(“写入文档时出错:\(错误)”)
}否则{
打印(“文档成功写入!”)
}
}
}