gpt4 book ai didi

swift - 如何在 swift 中进行顺序 firebase 查询?

转载 作者:行者123 更新时间:2023-11-28 06:32:09 26 4
gpt4 key购买 nike

下面是我的数据结构:

{    
"posts": {

"xyz1": {
"author": "Jan",
"uid": "abc123",
},
"xyz2": {
"author": "Jenny",
"uid": "abc456",
},

}

"users": {
"abc123": {
"email": "Jan@gmail.com",
"profilePicURL": "https://firebasestorage.googleapis.com/v0/b/",
},
"abc456": {
"email": "Jenny@gmail.com",
"profilePicURL": "https://firebasestorage.googleapis.com/v0/c/",
},
}
}

我想在表格 View 中显示“帖子”条目列表。

 let postRef = ref.child("posts")

postRef.observe(.childAdded, with: { (snapshot) in
let authorText = snapshot.value!.object(forKey: "author") as! String
let userIDText = snapshot.value!.object(forKey: "uid") as! String
}) { (error) in
print(error.localizedDescription)
}

我如何使用从上述查询中检索到的“uid”进行顺序查询,以使用“users”中的“uid”值检索“profilePicURL”。最终目标是在表格 View 中显示存储在帖子旁边的个人资料图片。

感谢您提供的任何帮助。

最佳答案

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell", for: indexPath) as! HomeTableViewCell

cell.author.text = String(self.author[(indexPath as NSIndexPath).row])
let userIDText = String(self.userID[(indexPath as NSIndexPath).row])

ref.child("users").child(userIDText).observeSingleEvent(of: .value, with: { (snapshot) in
print("snaphot is \(snapshot)")
let imageLink = snapshot.value?["profileImageUrl"] as! String
self.storageRef = FIRStorage.storage().reference(forURL: imageLink)

cell.profilePic.loadImageUsingCacheWithUrlString(urlString: imageLink)

}) { (error) in
print(error.localizedDescription)
}
return cell
}

我使用 UIImageView 的以下扩展来使用 URL 加载图像并且它有效!!

 let imageCache = NSCache<AnyObject, AnyObject>()

extension UIImageView {

func loadImageUsingCacheWithUrlString(urlString: String) {

self.image = nil

//check cache for image first
if let cachedImage = imageCache.object(forKey: urlString) as? UIImage {
self.image = cachedImage
return
}

//otherwise fire off a new download
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: url! as URL, completionHandler: { (data, response, error) in

//download hit an error so lets return out
if error != nil {
print(error)
return
}

DispatchQueue.main.async(execute: {

if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString)

self.image = downloadedImage
}
})

}).resume()
}

}

关于swift - 如何在 swift 中进行顺序 firebase 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40049845/

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