Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
筛选数据快照Swift Firebase_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

筛选数据快照Swift Firebase

筛选数据快照Swift Firebase,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我正在尝试从数据库返回数据,我已经成功地完成了。问题是我不希望所有的数据都有特征,只希望那些具有当前UID(存储在JSON树中)的数据有特征 这是我的JSON树,当前只有一个UID,但会有很多 user_posts- LDLc60j71FBvJGeYn5i description: "Write here" photoUrl: "https://firebasestorage.googleapis.com/v0/b/blo..." uid: "zQRxvM3

我正在尝试从数据库返回数据,我已经成功地完成了。问题是我不希望所有的数据都有特征,只希望那些具有当前UID(存储在JSON树中)的数据有特征

这是我的JSON树,当前只有一个UID,但会有很多

user_posts- 
  LDLc60j71FBvJGeYn5i 
     description: "Write here"
     photoUrl: "https://firebasestorage.googleapis.com/v0/b/blo..."
     uid: "zQRxvM3cwzQMewUtVamk8JQrEFJ3"
以下是返回数据库文件夹中所有数据的当前代码:

var posts2 = [user_posts]()
func loadPosts(){
    Database.database().reference().child("user_posts").observe(.childAdded) {(snapshot: DataSnapshot) in
        if let dict = snapshot.value as? [String: Any] {
            let descriptionText = dict["description"] as! String
            let photoUrlString = dict["photoUrl"] as! String
            let post = user_posts(descriptionText: descriptionText, photoUrlString: photoUrlString)
            self.posts2.append(post)
            self.myPageTableView.reloadData()
        }
    }
}


override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
    myPageTableView.dataSource = self
    myPageTableView.delegate = self
    loadPosts()

}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return posts2.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier:"myPageCell", for: indexPath)
        as! myPageTableViewCell
    cell.myPageDescription?.text = posts2[indexPath.row].description
    let photoUrl = posts2[indexPath.row].photoUrl
    let url = URL(string: photoUrl)
    cell.myPageImage.sd_setImage(with: url, placeholderImage: nil)
    return cell
    }
}

要仅加载特定用户的帖子,您需要使用Firebase查询:

let uid = Auth.auth().currentUser.uid
let posts = Database.database().reference().child("user_posts")
let query = posts.queryOrdered(byChild: "").queryEqual(toValue: uid)
query.observe(.childAdded) {(snapshot: DataSnapshot) in
  ...
另请参阅