从Firebase检索数据并将信息存储到表格视图中

时间:2019-09-27 01:10:41

标签: ios xcode firebase firebase-realtime-database

晚上好,我一直在尝试从Firebase获取用户信息,以发布到tableViewController上,这与instagrams通知供稿非常相似。我已经在这个问题上停留了一段时间,而且确实很难解决。我之前在这里曾问过这个问题,但我仍然没有找到解决方案。

我将尽力解释我的代码,以期找到解决方案。

以下是我将数据保存到Firebase的方式。

Here is how my data is saved in firebase.

这是我数据库的扩展

extension Database {

static func fetchPoster(with uid: String, completion: @escaping(Poster) -> ()) {

    USER_REF.child(uid).observeSingleEvent(of: .value) { (snapshot) in
        guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
        let poster = Poster(dictionary: dictionary)
        completion(poster)
    }
}

static func fetchUser(with postId: String, completion: @escaping(User) -> ()) {

    USER_REF.child(postId).observeSingleEvent(of: .value) { (snapshot) in
        guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
        guard let ownerUid = dictionary["fromId"] as? String else { return }

        Database.fetchUser(with: ownerUid, completion: { (user) in
            let user = User(dictionary: dictionary)
           // let user = User(postId: postId, user: user, dictionary: dictionary)
            completion(user)
        })
    }
}
}

在下面,这是我计划从Firebase调用信息以存储到我的notificationsViewController中的方式。

var notifications = [userNotifications]()

override func viewDidLoad() {
    super.viewDidLoad()
  fetchNotifications()
 }

    func fetchNotifications() {
    guard let currentUID = Auth.auth().currentUser?.uid else { return }
    USER_SWIPES_REF.child(currentUID).observe(.value) { (snapshot) in
        print(snapshot)

        guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
        guard let uid = dictionary["userSwipeId"] as? String else { return }

       Database.fetchUser(with: uid, completion: { (user) in
            if let postId = dictionary["fromId"] as? String {

                Database.fetchPoster(with: postId, completion: { (poster) in
                    let notification = userNotifications(poster: poster, dictionary: dictionary)
                    self.notifications.append(notification)
                    self.tableView.reloadData()
                })
            }

      })

    }
}

最后但并非最不重要的,这是我的用户和发帖人结构

//海报

init(dictionary: [String: Any]) {

    self.cost = dictionary["price"] as? String ?? ""
    self.description = dictionary["description"] as? String
    self.category = dictionary["category"] as? String ?? ""
    self.imageUrl1 = dictionary["imageUrl1"] as? String ?? ""
    self.imageUrl2 = dictionary["imageUrl2"] as? String
    self.imageUrl3 = dictionary["imageUrl3"] as? String
    self.fromId = dictionary["fromId"] as? String ?? ""
}

//用户

    init(dictionary: [String: AnyObject]) {
    self.number = dictionary["phonenumber"] as? String
    self.id = dictionary["uid"] as? String
    self.name = dictionary["name"] as? String
    self.email = dictionary["email"] as? String
}

在我的notificationsViewController中,我要发布一组虚拟数据,因此在numberOfRowsInsection中,它将根据“滑动用户数”返回“空白用户滑动您的信息”。当我将return设置为5时,它将返回5个伪数据。在我的控制台中,我可以看到firebase的打印语句,其中显示有多少用户已刷卡。但是,当我单击viewController本身时,它只是空白。如果有人可以帮助我,我将非常感谢。

0 个答案:

没有答案