【问题标题】:Errors trying to fetch users from firebase [closed]尝试从 firebase 获取用户时出错 [关闭]
【发布时间】:2020-06-28 22:25:14
【问题描述】:

我正在尝试从我的 Firebase 数据库中获取用户,在 UITableView 上显示他们的姓名并能够搜索他们。出于某种原因,我在此函数中看到两个错误以获取用户。

  1. 错误:'user' 类型的值在'self.users.append(user)' 行没有成员'append'
  2. 错误:没有“dispatch_get_main_queue”候选者在“dispatch_async(dispatch_get_main_queue)”行产生预期的上下文结果类型“(DispatchQueue, () -> Void)”

如果您知道任何解决此问题的方法,请告诉我。我已经坚持了一段时间,似乎无法让用户显示。谢谢。

    func fetchUser() {
    Database.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
        
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = User()
            
            //will crash if your class properties don't exactly match up with firebase dictionary keys
            user.setValuesForKeys(dictionary)
            self.users.append(user)
            
            
            //this will crash because of background thread, so use dispatch async to fix
            dispatch_async(dispatch_get_main_queue)
            self.tableView.reloadData()
            print(user.name!, user.email!)

【问题讨论】:

  • 我昨天已经就你的问题评论了这个确切的问题:stackoverflow.com/questions/62616575/…。将来如果评论不清楚,请回复它而不是重新发布相同的问题。
  • 很抱歉,感谢您昨天的回复。该帖子已被关闭,因为它的格式不正确,我没有看到您的回复。我的坏,新来的。再次感谢您的回答

标签: ios swift database firebase


【解决方案1】:

第二个错误是因为这不是在主线程上运行某些东西的正确语法。应该是

DispatchQueue.main.async { 
    self.tableView.reloadData()
}

至于第一个错误,我只能推测(因为您没有提供self.users 的定义方式。但我怀疑您将其定义为var users: User,而不是数组。所以请确保您有:

var users: [User]

或初始化:

var users = [User]()

【讨论】:

    猜你喜欢
    • 2021-03-22
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2022-06-14
    • 2020-07-21
    • 2023-04-09
    • 2018-11-08
    • 2010-09-24
    相关资源
    最近更新 更多