以下是我的数据结构:
{
"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/",
},
}
}
我想显示"帖子列表" tableview中的条目。
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"从上面的查询中检索以进行顺序查询以检索" profilePicURL"使用" uid" "用户"中的价值。最终目标是在tableview中显示除帖子之外存储的profilePic。
感谢您提供的任何帮助。
答案 0 :(得分:1)
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()
}
}
答案 1 :(得分:0)
最好的办法是将不同的用户存储到用户为结构的用户数组中。
struct User {
var name: String = ""
var id: String = ""
}
然后在ViewController中下载Firebase中的内容并创建用户结构的模型。
let users: [User] = []
yourFireBaseQueryFunc() {
...
postRef.observe(.childAdded, with: { (snapshot) in
for item in snapshot {
let name = snapshot.value!.object(forKey: "author") as! String
let id = snapshot.value!.object(forKey: "uid") as! String
let user = User(name: name, id: id)
users.append(user)
}
然后例如在tableView
中,您从模型数组中取出indexPath和一个Model并调用函数从Firebase获取图像链接:
cellForRowAtIndexPath... {
let user = users[indexPath.row]
let image = getImage(user.id)
let imgURL = NSURL(string: post.picture)
cell.yourImageView.sd_setImageWithURL(imgURL)
}
然后查询图片:
func getImage(userID: String) -> String {
var imageLink: String = ""
let ref = firebase.child("users").child(userID)
ref.observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
imageLink = snapshot.value!.valueForKey("profilePicURL") as! String
}
})
return imageLink
}