gpt4 book ai didi

ios - 如何使用 Firestore 订购数据以在 iOS 上显示

转载 作者:行者123 更新时间:2023-11-30 10:25:21 27 4
gpt4 key购买 nike

我正在尝试按时间戳排序我的数据,但它似乎不起作用。我的 Feed 类在下面。有人可以看一下并告诉我发生了什么事吗?我相信问题出在 checkForUpdates() 函数中。我已尽一切努力让数据按时间戳排序。当我删除 loadData() 函数时,应用程序按时间戳顺序显示帖子,但新帖子始终位于 View 的末尾。 Feed not ordered by timnestamp

导入UIKit导入FirebaseFirestore

FeedVC 类:UITableViewController {

var db = Firestore.firestore()
var postArray = [Posts]()

override func viewDidLoad() {
super.viewDidLoad()

//db = Firestore.firestore()

//加载数据() //checkForUpdates()

}
override func viewDidAppear(_ animated: Bool){
checkForUpdates()
loadData()



}

func loadData() {
db.collection("posts").getDocuments() {
querySnapshot, error in
if let error = error {
print("\(error.localizedDescription)")
}else{
self.postArray = querySnapshot!.documents.flatMap({Posts(dictionary: $0.data())})
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}


}

func checkForUpdates() {
db.collection("posts").order(by: "timeStamp", descending: true)
.addSnapshotListener {
querySnapshot, error in

guard let snapshot = querySnapshot else {return}

snapshot.documentChanges.forEach {
diff in

if diff.type == .added {
self.postArray.append(Posts(dictionary: diff.document.data())!)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}

}
}

@IBAction func composePost(_ sender: Any) {





let composeAlert = UIAlertController(title: "New Post", message: "Enter your name and message", preferredStyle: .alert)

composeAlert.addTextField { (textField:UITextField) in
textField.placeholder = "Your name"
}

composeAlert.addTextField { (textField:UITextField) in
textField.placeholder = "Your message"
}

composeAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

composeAlert.addAction(UIAlertAction(title: "Send", style: .default, handler: { (action:UIAlertAction) in do {


if let name = composeAlert.textFields?.first?.text, let content = composeAlert.textFields?.last?.text {

let newSweet = Posts(name: name, content: content, timeStamp: Timestamp())

var ref:DocumentReference? = nil
ref = self.db.collection("posts").addDocument(data: newSweet.dictionary) {
error in

if let error = error {
print("Error adding document: \(error.localizedDescription)")
}else{
print("Document added with ID: \(ref!.documentID)")
}

}

}
}

}))

self.present(composeAlert, animated: true, completion: nil)


}



// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {

return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return postArray.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

let post = postArray[indexPath.row]

cell.textLabel?.text = "\(post.name): \(post.content)"
cell.detailTextLabel?.text = "\(post.timeStamp.dateValue())"

return cell
}

}

最佳答案

加载数据时使用 order by:

chats.order(by: "createdTime", descending: false) 

关于ios - 如何使用 Firestore 订购数据以在 iOS 上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60088131/

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