2017-02-16 3 views
0

現在、Firebaseデータベースの投稿をセルに表示するUITableViewがあります。私がログインしているユーザからの投稿のみを表示したいと思い、別の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 
} 

も私の写真ですFirebaseに意味:

Here

答えて

0

Firebaseは、NoSQLのデータベースです。 SQLのプラクティスをそれに投影しようとすると、あなたに悲しみが生じます。

投稿の長いリストを1つ保存してからフィルタリングするクエリを使用する代わりに、各ユーザの投稿をユーザ固有のノードの下に保存します。私。

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データベースでは正常です。 –

関連する問題