2017-02-16 107 views
0

我目前有一個UITableView在單元格的Firebase數據庫中顯示帖子。還有,我想顯示從誰已登錄用戶只帖子另一個UITableView的根據用戶名顯示Firebase數據

這是我目前在我的UserPostViewController:

func loadData() { 
    let uid = FIRAuth.auth()?.currentUser?.uid 
    FIRDatabase.database().reference().child("posts").child(uid!).observeSingleEvent(of: .value, with: { 
     (snapshot) in 
     if let postsDictionary = snapshot.value as? [String: AnyObject] { 
      for post in postsDictionary { 
       self.userPosts.add(post.value) 
      } 
      self.userPostsTableView.reloadData() 
     } 
    }) 
} 

// MARK: - Table view data source 

func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    // pass any object as parameter, i.e. the tapped row 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return self.userPosts.count 
} 

// Displays posts in postsTableView 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! PostTableViewCell 
    // Configure the cell... 
    let post = self.userPosts[indexPath.row] as! [String: AnyObject] 
    cell.titleLabel.text = post["title"] as? String 
    cell.priceLabel.text = post["price"] as? String 
    if let imageName = post["image"] as? String { 
     let imageRef = FIRStorage.storage().reference().child("images/\(imageName)") 
     imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in if error == nil { 
      let image = UIImage(data: data!) 
      cell.titleLabel.alpha = 0 
      cell.contentTextView.alpha = 0 
      cell.postImageView.alpha = 0 
      UIView.animate(withDuration: 0.4, animations: { 
       cell.titleLabel.alpha = 1 
       cell.contentTextView.alpha = 1 
       cell.postImageView.alpha = 1 
      }) 
     } else { 
      print("Error occured during image download: \(error?.localizedDescription)") 
      } 
     }) 
    } 
    return cell 
} 

這裏也的照片是我意味着火力地堡:

Here

回答

0

火力地堡是一個的NoSQL數據庫。嘗試在它上面投射SQL實踐會導致你悲傷。

而不是存儲一個長的職位列表,然後使用查詢過濾,而是存儲每個用戶在用戶特定節點下的帖子。即

users 
    $uid 
    username: "gabe" 
posts 
    $uid 
    -K12348: { 
     description: "...." 
     image: "...." 
    } 

現在你可以不斷增長的清單上訪問的職位爲特定用戶提供直接訪問讀取,而不是查詢:

FIRDatabase.database().reference().child("posts").child(uid!).observeSingleEvent(of: .value, with: { 
    (snapshot) in 
    if let postsDictionary = snapshot.value as? [String: AnyObject] { 
     for post in postsDictionary { 
      self.userPosts.add(post.value) 
     } 
     self.userPostsTableView.reloadData() 
    } 

請發表您的JSON文本未來時間,以便我可以將其複製/粘貼到我的答案中。

我推薦閱讀NoSQL data modeling並查看Firebase for SQL developers

+0

我完全理解你的想法,但是如果我要在我的數據庫中實現這一點,那麼將無法調用UITableView中的所有帖子 – gabe

+0

這是正確的。這就是爲什麼你爲每個用例建模數據的原因。通常你會最終在多個地方複製數據。這在NoSQL數據庫中是正常的。 –