2017-03-10 3 views
0

내 네트워킹 파일 및 homeviewController에서 fetchAllPosts 함수를 어떻게 변경하여 post.UID를 따르는 사용자의 UID와 일치하는 게시물 만 데이터베이스에서 가져 와서 내 피드가 게시물로 채워지도록하십시오 내가 팔로우하는 사용자의 여기 Firebase 데이터베이스의 데이터를 어떻게 필터링합니까?

let followingRef = "following/" + (self.loggedInUserData?["uid"] as! String) + "/" + (self.otherUser?["uid"] as! String) 

가있는 현재 fetchAllPosts 함수 인 중포 기지 데이터베이스 여기

posts 
    -Ke4gQKIbow10WdLYMTL (generated key) 
     postDate: 
     postId: 
     postPicUrl: 
     postText: 
     postTit: 
     type: 
     uid: looking to match this 

의 포스트 구조이다 : 여기

는 참조 I 데이터베이스에서 새로운 팔로워을 어떻게 보이고 네트워킹 파일

func fetchAllPosts(completion: @escaping ([Post])->()) { 

    let postRef = self.dataBaseRef.child("posts") 
    postRef.observe(.value, with: { (posts) in 

     var resultsArray = [Post]() 
     for post in posts.children { 

      let post = Post(snapshot: post as! FIRDataSnapshot) 
      resultsArray.append(post) 

     } 

     completion(resultsArray) 

    }) { (error) in 
     print(error.localizedDescription) 
    } 

    } 

여기에 homeVi의 fetchAllPosts 기능이 있습니다. ewController

다음
private func fetchAllPosts(){ 
    authService.fetchAllPosts {(posts) in 
     self.postsArray = posts 
     self.postsArray.sort(by: { (post1, post2) -> Bool in 
      Int(post1.postDate) > Int(post2.postDate) 
     }) 

     self.tableView.reloadData() 
    } 
} 

내 빠른 파일 내 게시물 구조는 다음과 같습니다

struct Post { 


    var username: String! 
    var uid: String! 
    var postId: String! 
    var type: String 
    var postText: String 
    var postTit: String 
    var postPicUrl: String! 
    var postDate: NSNumber! 
    var ref: FIRDatabaseReference! 
    var key: String = "" 

    init(postId: String,postText: String, postTit:String, postDate:NSNumber, postPicUrl: String?, type:String, uid: String, key: String = ""){ 

     self.postText = postText 
     self.postTit = postTit 
     self.postPicUrl = postPicUrl 
     self.type = type 
     self.uid = uid 
     self.postDate = postDate 
     self.postId = postId 

    } 

    init(snapshot: FIRDataSnapshot){ 

     self.ref = snapshot.ref 
     self.key = snapshot.key 
     self.postId = (snapshot.value! as! NSDictionary)["postId"] as! String 
     self.type = (snapshot.value! as! NSDictionary)["type"] as! String 
     self.postPicUrl = (snapshot.value! as! NSDictionary)["postPicUrl"] as! String 
     self.postDate = (snapshot.value! as! NSDictionary)["postDate"] as! NSNumber 
     self.postTit = (snapshot.value! as! NSDictionary)["postTit"] as! String 
     self.uid = (snapshot.value! as! NSDictionary)["uid"] as! String 
     self.postText = (snapshot.value! as! NSDictionary)["postText"] as! String 
    } 


    func toAnyObject() -> [String: Any] { 
     return ["postId":self.postId,"type":self.type, "postPicUrl":self.postPicUrl,"postDate":self.postDate, "postTit":self.postTit,"uid": self.uid, "postText":self.postText,] 
    } 
} 

답변

0

당신이 사용 했;

postRef.queryOrdered(byChild: "uid").queryEqual(toValue: yourUid).observe(.value, with: { 
        snapshot in 

       if let shot = snapshot { 
        let post = Post(snapshot: post as! FIRDataSnapshot) 
       } 

    } 

이렇게하면 찾고있는 데이터가 반환됩니다.

관련 문제