2017-09-01 10 views
0

の投稿が好きかどうかを効率的に判断するユーザーが投稿を好きな場所にアプリを持っていて、現在のユーザーが過去に効率的に投稿を好きだったかどうかを判断したい。私のデータは現在、次のようになります。しかしFirebase

if let people = post["peopleWhoLike"] as? [String: AnyObject] { 
     if people[(Auth.auth().currentUser?.uid)!] != nil { 
      posst.userLiked = true 
     } 
} 

Post Data

また、私はこれをやっている私の現在のクエリで

User Likes

すべてのユーザーのための好みを保存します私は、これは非常に効率的ではない投稿のすべての好きなものをダウンロードする必要があると私は信じて、これを試した:

if (post["peopleWhoLike\(Auth.auth().currentUser!.uid)"] as? [String: AnyObject]) != nil { 
    posst.userLiked = true 
} 

2番目の方法は正しく動作していないようです。これを行うより良い方法はありますか?

pagingReference.child("posts").queryLimited(toLast: 5).observeSingleEvent(of: .value, with: { snap in 
     for child in snap.children { 
      let child = child as? DataSnapshot 
      if let post = child?.value as? [String: AnyObject] { 
       let posst = Post() 
       if let author = post["author"] as? String, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String, let postDescription = post["postDescription"] as? String, let timestamp = post["timestamp"] as? Double, let category = post["category"] as? String, let table = post["group"] as? String, let userID = post["userID"] as? String, let numberOfComments = post["numberOfComments"] as? Int, let region = post["region"] as? String, let numLikes = post["likes"] as? Int { 
+0

、これをチェックアウト:https://www.youtube.com/watch?v=HjlQH3RsGcUの真ん中にビデオフランクはそれについて何か言います。 https://stackoverflow.com/questions/41527058/many-to-many-relationship-in-firebaseはおそらくあなたが使用できるものです。私はより迅速にクエリするためにあなたのデータベースを非正規化すべきだと思います。はい、この方法で重複データを取得します。 –

+0

そのようなデータが投稿とuserActivityに保存されているため、データは既に非正規化されています... – DoesData

答えて

0

それを解決:

私は直接言っていた値を照会してから表示するためにどのようなボタン決定のtableViewで

ここでも私の最初のクエリがあります。私のtableViewに呼び出され

static func userLiked(postID: String, cell: BetterPostCell, indexPath: IndexPath) { 
     // need to cache these results so we don't query more than once 
     if newsfeedPosts[indexPath.row].userLiked == false { 
      if let uid = Auth.auth().currentUser?.uid { 
       likeRef.child("userActivity").child(uid).child("likes").child(postID).queryOrderedByKey().observeSingleEvent(of: .value, with: { snap in 
        if snap.exists() { 
         newsfeedPosts[indexPath.row].userLiked = true 
         cell.helpfulButton.isHidden = true 
         cell.notHelpfulButton.isHidden = false 
        } 
       }) 
       likeRef.removeAllObservers() 
      } 
     } 
    } 

:これは、多くの関係に多くのと呼ばれる

DatabaseFunctions.userLiked(postID: newsfeedPosts[indexPath.row].postID, cell: cell, indexPath: indexPath)