2016-10-14 28 views
1

下面是我的數據結構:如何在swift中進行順序firebase查詢?

{  
    "posts": { 

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

     } 

    "users": { 
     "abc123": { 
      "email": "[email protected]", 
      "profilePicURL": "https://firebasestorage.googleapis.com/v0/b/", 
     }, 
     "abc456": { 
      "email": "[email protected]", 
      "profilePicURL": "https://firebasestorage.googleapis.com/v0/c/", 
     }, 
     }  
} 

我想顯示「上崗」的實現代碼如下條目列表。

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

最好的辦法是將不同的用戶存儲到其中用戶是Struct的用戶數組中。

struct User { 
    var name: String = "" 
    var id: String = "" 
} 

然後在您的ViewController中,您從Firebase下載內容並創建User Struct的模型。

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和一個型號出你的模型排列,並調用一個函數來得到你的火力地堡圖片鏈接:

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 
} 
+0

嗨大衛,謝謝你的及時回覆。但是,您的回覆並不能真正解決我的問題。 –

+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加載採用t的圖像他的網址,它的工作!

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() 
     } 

    }