gpt4 book ai didi

swift - 在 Collection View 中随机上传帖子 - Swift 和 Firebase

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

我一直在重构我的代码,但现在我在帖子上遇到了问题。

每当我将新帖子添加到 Collection View 时,它都会无序地添加到随机单元格中,而不是添加到第一篇帖子中。

我知道原因是 fetchuser 函数,并且从我被告知的异步加载情况来看,但不知道该怎么做才能纠正这个问题。

有人可以帮我弄清楚该怎么做才能将我的帖子添加到第一个单元格中吗?

 @objc func observePostsAdoption() {
let postsRef = Database.database().reference().child("posts")
postsRef.queryOrdered(byChild: "postType").queryEqual(toValue: "adopt").observe(.value) { (snapshot) in
var tempPost = [Posts]()
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot {
let dict = childSnapshot.value as? [String: Any]
let newAdoptiondPost = Posts.transformPost(dict: dict!)
//This will look up all users at once
self.fetchUser(userid: newAdoptiondPost.userid!, completed: {
tempPost.insert(newAdoptiondPost, at: 0)
DispatchQueue.main.async {
self.postsadoption = tempPost
self.adoptionCollectionView.reloadData()
self.refresherAdoption.endRefreshing()
}
})
}

}
}
}

func fetchUser(userid: String, completed: @escaping ()-> Void ) {

Database.database().reference().child("users").child(userid).observeSingleEvent(of: .value) { (snapshot) in
if let dict = snapshot.value as? [String: Any] {
let user = UserProfile.transformUser(dict: dict)
self.users.insert(user, at: 0)
completed()
}
}

}

这是我的帖子结构

class Posts {

//UserView
var uid: String?
var author: UserProfile?
var timestamp: Date?
var userid: String?

func getDateFormattedString() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, HH:mm"
return formatter.string(from: self.timestamp!)
}

//Image
var photoUrl: URL?

//PostInformation View
var city: String?
var municipality: String?
var name: String?
var breed : String?
var phone : String?
var address : String?
var petType: String?
var genderType: String?
var comments: String?

}

扩展帖子{

static func transformPost(dict: [String: Any]) -> Posts {

let post = Posts()

//Post Picture
let photoUrl = dict["photoUrl"] as? String
post.photoUrl = URL(string: photoUrl!)

//INFO POSTS
post.userid = dict["userid"] as? String
post.city = dict["city"] as? String
post.municipality = dict["municipality"] as? String
post.name = dict["name"] as? String
post.breed = dict["breed"] as? String
post.phone = dict["phone"] as? String
post.address = dict["address"] as? String
post.comments = dict["comments"] as? String
post.petType = dict["petType"] as? String
post.genderType = dict["gender"] as? String
let timestamp = dict["timestamp"] as? Double
post.timestamp = Date(timeIntervalSince1970: timestamp!/1000)

return post

}

}

最佳答案

如果您已经有按帖子类型排序的帖子,您可以根据时间戳进行排序。例如

@objc func observePostsAdoption() {
let postsRef = Database.database().reference().child("posts")
postsRef.queryOrdered(byChild: "postType").queryEqual(toValue: "adopt").observe(.value) { (snapshot) in
var tempPost = [Posts]()
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot {
let dict = childSnapshot.value as? [String: Any]
let newAdoptiondPost = Posts.transformPost(dict: dict!)
//This will look up all users at once
self.fetchUser(userid: newAdoptiondPost.userid!, completed: {
tempPost.insert(newAdoptiondPost, at: 0)
DispatchQueue.main.async {
self.postsadoption = tempPost
self.postsadoption.sort { (p1, p2) -> Bool in
return p1.timeStamp?.compare(p2.timeStamp!) == .orderdDescending
}
self.adoptionCollectionView.reloadData()
self.refresherAdoption.endRefreshing()
}
})
}

}
}
}

这样,帖子采用数组将根据您拥有的时间戳进行排序。

关于swift - 在 Collection View 中随机上传帖子 - Swift 和 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53327406/

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