原因是我的代码编写得不太好,而且我想不出更好的方法。
当前,我正在使用For语句从集合中检索特定文档。然后,我将结果分为2个单独的数组(“已完成”和“仍待完成”)。
现在,如果文档进行了更改,例如,某个用户完成了工作……它将从部件阵列中删除并追加到部件部件阵列中。 但是,我目前在TableView中出现了更改的文档中出现的多个对象。 任何人都可以建议一种更好的方式来检索此信息吗?
func getPartInfo(handleComplete: @escaping () -> Void) {
self.parts.removeAll()
self.partsCompl.removeAll()
for eachPart in (job?.jobArray)! {
self.partsListener = firestore.collection(PARTINFO_REF).document(eachPart)
.addSnapshotListener { documentSnapshot, error in
guard let document = documentSnapshot else {
//print("Error fetching document: \(error!)")
return
}
self.partInfo = PartInfo.parseDocData(documentSnapshot: document)
let array = self.jobData?.jobCompl ?? [""]
let issueDoc = self.partInfo?.documentId ?? ""
if array.contains(issueDoc) {
self.partsCompl.append(self.partInfo!)
print("PART HAS BEEN COMPLETED")
} else {
self.parts.append(self.partInfo!)
print("PART HAS NOT BEEN COMPLETED")
}
self.tableView.reloadData()
let totalCount = self.parts.count + self.partsCompl.count
if totalCount == self.job?.jobArray.count {
handleComplete()
} else {
print("Still more to complete")
}
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "remJob") as! RemJobTableViewCell
let storage = Storage.storage()
switch segmentControl.selectedSegmentIndex {
case 0:
if job?.jobType == "Ins" {
let imageFOr = NSURL(string: parts[indexPath.row].partImage)
ImageService.getImage(withURL: imageFOr! as URL) { image in
cell.issueImage?.image = image
}
cell.issueTitle.text = parts[indexPath.row].partNumber ?? ""
cell.partNumberLabel.text = parts[indexPath.row].type ?? ""
cell.locationLabel.text = "\(parts[indexPath.row].tagID ?? "") | \(parts[indexPath.row].area ?? "")"
return cell
case 1:
if job?.jobType == "Ins" {
let imageFOr = NSURL(string: partsCompl[indexPath.row].partImage)
ImageService.getImage(withURL: imageFOr! as URL) { image in
cell.issueImage?.image = image
}
cell.issueTitle.text = partsCompl[indexPath.row].partNumber ?? ""
cell.partNumberLabel.text = partsCompl[indexPath.row].type ?? ""
cell.locationLabel.text = "\(partsCompl[indexPath.row].tagID ?? "") | \(partsCompl[indexPath.row].area ?? "")"
return cell