仅显示登录用户的swift/xcode/firebase中的帖子数据 [英] Showing post data from logged in user only swift/xcode/firebase

查看:54
本文介绍了仅显示登录用户的swift/xcode/firebase中的帖子数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我一直在在线上尝试教程,并尝试制作一个社交媒体应用程序,并且该应用程序可以很好地运行,因为用户发布的所有帖子都填充在一个场景中,但是在另一个场景中,我希望当前登录的用户能够只能查看其帖子,而不必从其他用户发布的一堆帖子中查找其帖子,以下是我的代码示例:

Hey I have been following tutorials online to try and make a social media app and it works fine where all the posts made by users populates in one scene but in another scene I would like for the users who are currently logged in to be able to see their posts only instead of having to find their posts from a bunch of posts made by other users, below are my code samples:

邮编:

import Foundation
import UIKit
import Firebase


class Posts{

var id: String
var author: UserProfile
var text: String
var timestamp:Double
var createdAt:Date


init(id:String, author:UserProfile,text:String,timestamp:Double) {
    self.id = id
    self.author = author
    self.text = text
    self.timestamp = timestamp
    self.createdAt = Date(timeIntervalSince1970: timestamp / 1000)
}


static func parse(_ key:String, _ data:[String:Any]) -> Posts? {

    if let author = data["author"] as? [String:Any],
        let uid = author["uid"] as? String,
        let name = author["name"] as? String,
        let photoURL = author["photoURL"] as? String,
        let url = URL(string:photoURL),
        let text = data["text"] as? String,
        let timestamp = data["timestamp"] as? Double {

        let userProfile = UserProfile(uid: uid, name: name, photoURL: url)
        return Posts(id: key, author: userProfile, text: text, timestamp:timestamp)


        func performDeepQuery() {
             guard let user = Auth.auth().currentUser else { return }
            let uid = user.uid
            let ref = Database.database().reference().child("posts")
            let query = ref.queryOrdered(byChild: "author/uid").queryEqual(toValue: uid)
            query.observeSingleEvent(of: .value, with: { snapshot in
                let allPosts = snapshot.children.allObjects as! [DataSnapshot]
                for postSnap in allPosts {

                    let text = postSnap.childSnapshot(forPath: "text").value as? String ?? "No Text"
                    print(text)
                   //same as above
                }
            })
        }



  }

  return nil }
 }

func numberOfSections(in tableView: UITableView) -> Int {
     return 2

 }

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     switch section {
            case 0:
                return theposts.count
            case 1:
                return fetchingMore ? 1 : 0
            default:
                return 0
            }    }


 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 { let cell = HomeViewControllerScene.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! Posting_TableViewCell
     cell.set(theposts: theposts[indexPath.row])
     return cell}
 else {
         let cell = HomeViewControllerScene.dequeueReusableCell(withIdentifier: "loadingCell", for: indexPath) as! LoadingCell
         cell.spinner.startAnimating()
         return cell
     }
 }


 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {


     cellHeights[indexPath] = cell.frame.size.height
 }


 func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return cellHeights[indexPath] ?? 72.0
    }

请有人在这里帮助我,我已经为此挠了一段时间!!!

Please someone help me out here I have been scratching my head for a while with this!!!

推荐答案

我相信问题是如何获取特定用户的帖子.该结构看起来不错,但无需在每个帖子中都有一个子节点作者",所以代替这个:

I believe the question is how to get the posts for a certain user. The structure looks good but no need to have a child node 'author' in each post so instead of this:

posts
   post_id_0
      author
         author data
      text: "Hello, World"
      uid: "uid_0"

执行此操作

posts
   post_id_0
      text: "Hello, World"
      uid: "uid_0"
      name: "usmaan"
      photoURL:"https://firebasestorage..."

因此,现在只需查询该用户的帖子(这是针对Firestore的,请向下滚动RTDB解决方案)...

So now just query for this users posts (this is for Firestore, scroll down fo the RTDB solution)...

func getThisUsersPosts() {
    let uid = "uid_0" //this users uid
    self.db.collection("posts]").whereField("uid", isEqualTo: uid).getDocuments { (snapshot, error) in
        if let err = error {
            print(err.localizedDescription)
            return
        }

        if let doc = snapshot?.documents {
            for d in doc {
                let text = d.get("text") as? String ?? "No Post Text"
                print(text)
            }
        } else {
            print("no posts found")
        }
    }
}

self.db指向我的Firestore.

self.db points to my Firestore.

OP正在使用实时数据库,因此这是该代码

OP is using the Real Time Database so here's the code for that

func getThisUsersPosts() {
    let uid = "uid_0"
    let ref = self.ref.child("posts") //self.ref points to MY firebase.
    let query = ref.queryOrdered(byChild: "uid").queryEqual(toValue: uid)
    query.observeSingleEvent(of: .value, with: { snapshot in
        let allPosts = snapshot.children.allObjects as! [DataSnapshot]
        for postSnap in allPosts {
            let text = postSnap.childSnapshot(forPath: "text").value as? String ?? "No Text"
            print(text)
        }
    })
}

OP希望保持相同的结构.

OP wants to keep their same structure.

要查询两个级别的数据,我们使用所谓的深度查询",其外观将如下所示:

To query for data that's two levels deep we use what's called Deep Query and will look something like this:

func performDeepQuery() {
    let uid = "uid_0"
    let ref = self.ref.child("posts")
    let query = ref.queryOrdered(byChild: "author/uid").queryEqual(toValue: uid)
    query.observeSingleEvent(of: .value, with: { snapshot in
        let allPosts = snapshot.children.allObjects as! [DataSnapshot]
        for postSnap in allPosts {
           //populate your tableView datasource here
           let post = PostClass()
           post.postId = postSnap.key
           post.name = postStap.childSnapshot("name").value as? String ?? "No Post Name"
           post.text = postStap.childSnapshot("text").value as? String ?? "No Post Text"
           self.postArray.append(post)
        }
        self.myTableView.reloadData()
    })
}

将对此结构执行深度查询

which will perform a deep query on this structure

posts
   post_0
      author
         uid: "uid_0"
      name: "post 0 name"
      text: "post 0 text"

PostClass可能是这个

The PostClass could be this

class PostClass {
   var postId = ""
   var name = ""
   var text = ""
}

更多信息:

要获取当前用户的uid(也在入门指南中介绍),并假设您已通过身份验证(否则它将为nil)

To get the current users uid (which is covered in the getting started guide as well) and assuming you are authenticated (otherwise it will be nil)

guard let user = Auth.auth().currentUser else { return }
let uid = user.uid

这篇关于仅显示登录用户的swift/xcode/firebase中的帖子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆