gpt4 book ai didi

ios - 在显示 tableview 之前尝试完成 Firebase 存储下载的问题

转载 作者:行者123 更新时间:2023-11-28 14:57:21 26 4
gpt4 key购买 nike

我有一个 TableView ,根据单元格类,它将从 Firebase 下载图像。我注意到在使用该应用程序时,具有相同单元格标识符的单元格将在显示新图像之前显示之前下载的图像。这是我更改之前的内容。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableData[indexPath.row]["Image"] != nil {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageNotesData", for: indexPath) as! ImageNotesCell
cell.notes.delegate = self
cell.notes.tag = indexPath.row
cell.notes.text = tableData[indexPath.row]["Notes"] as! String
guard let imageFirebasePath = tableData[indexPath.row]["Image"] else {
return cell }
let pathReference = Storage.storage().reference(withPath: imageFirebasePath as! String)
pathReference.getData(maxSize: 1 * 1614 * 1614) { data, error in
if let error = error {
print(error)
} else {
let image = UIImage(data: data!)
cell.storedImage.image = image
}
}
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "notesData", for: indexPath) as! NotesCell
//let noteString = tableData[indexPath.row]["Notes"] as! String
cell.notes.text = tableData[indexPath.row]["Notes"] as! String
cell.notes.delegate = self
cell.notes.tag = indexPath.row
return cell
}
}

我知道这不是一个好的用户体验,而且看起来很笨重,因此我尝试将 pathReference.getData 移动到我设置数据的位置,但 View 在我的图像下载完成之前出现。我曾尝试使用完成处理程序,但我仍然遇到问题。

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
getSectionData(userID: userID, city: selectedCity, completion: {(sectionString) in
self.setupTableCellView(userID: userID, city: selectedCity, section: sectionString) { (tableData) in
DispatchQueue.main.async(execute: {
self.cityName?.text = selectedCity
self.changeSections.setTitle(sectionString, for: .normal)
self.currentSectionString = sectionString
self.setupTableData(tableDataHolder: tableData)
})
}
})
}

func setupTableCellView(userID: String, city: String, section: String, completion: @escaping ([[String:Any]]) -> () ) {
let databaseRef = Database.database().reference().child("Users").child(userID).child("Cities").child(city).child(section)
var indexData = [String:Any]()
var indexDataArray = [[String:Any]]()
databaseRef.observeSingleEvent(of: .value, with: { (snapshot) in
for dataSet in snapshot.children {
let snap = dataSet as! DataSnapshot
//let k = snap.key
let v = snap.value
indexData = [:]
for (key, value) in v as! [String: Any] {
//indexData[key] = value
if key == "Image" {
//let pathReference = Storage.storage().reference(withPath: value as! String)
print("before getImageData call")
self.getImageData(pathRef: value as! String, completion: {(someData) in
print("before assigning indexData[key]")
indexData[key] = someData
print("after assigning indexData[key]")
})
} else {
indexData[key] = value
}
}
indexDataArray.append(indexData)
}
completion(indexDataArray)
})
}

func getImageData(pathRef: String, completion: @escaping(UIImage) -> ()) {
let pathReference = Storage.storage().reference(withPath: pathRef as! String)
pathReference.getData(maxSize: 1 * 1614 * 1614, completion: { (data, error) in
if let error = error {
print(error)
} else {
let image = UIImage(data:data!)
print("called before completion handler w/ image")
completion(image!)
}
})
}

我不知道我是否以正确的方式处理这个问题,但我认为我是。我还猜测 getData 调用是异步的,这就是它总是在显示表格 View 后下载的原因。

最佳答案

你不能这样做。

从 Firebase 发出请求。

随着时间的推移,您会收到很多回复——所有的信息和所有变化的信息。

当每个新项目到达时 - 并且不要忘记它可能是添加或删除 - 更改您的表格以显示所有当前项目。

这就是 OCC!

OCC 是“偶尔连接的计算”。类似的短语是“离线优先计算”。因此,每当您使用任何您每天使用的主要服务(如 Facebook、Snapchat 等)时,这就是“OCC”:无论您是否有带宽,一切都会正确同步。你知道?当前主要的设备-云计算范式。

关于ios - 在显示 tableview 之前尝试完成 Firebase 存储下载的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49202256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com