Swift 无法识别 Firebase 用户

Swift Not Recognizing Firebase Users

我对编码还很陌生,正在尝试学习如何从 Firebase 获取我的用户信息!

我最近添加了代码以从 firebase 获取经度和纬度坐标。不幸的是,当我 运行 应用程序时,我再也看不到视图控制器中的用户了。在添加代码之前,用户会完美显示。`

func retrieveUsers(){
    //this is where you will also have to call the users location
    let ref = Database.database().reference()
    ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
        let users = snapshot.value as! [String: AnyObject]
        self.user.removeAll()
        for (_,value) in users {
            if let uid = value["uid"] as? String {
                if uid != Auth.auth().currentUser!.uid {
                    let userToShow = User()
                    if let fullName = value["full name"] as? String, let imagePath = value["urlToImage"] as? String
                        ,let userLongitude = value["long"] as? String, let userLatitude = value["lat"] as? String
                        //for some reason adding this makes it impossible to get code
                        //userLongitude and userLatitude cause no users to show up


                    {
                        userToShow.fullName = fullName
                        userToShow.imagePath = imagePath
                        userToShow.userID = uid
                        userToShow.userLongitude = userLongitude
                        userToShow.userLatitude = userLatitude

                        self.user.append(userToShow)
                    }
                }
            }
        }
        self.tableview.reloadData()
    })

    ref.removeAllObservers()

}
`

这里有更多与这个问题相关的代码:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UserCell

    cell.nameLabel.text = self.user[indexPath.row].fullName
    cell.userID = self.user[indexPath.row].userID
    cell.userImage.downloadImage(from: self.user[indexPath.row].imagePath!)
    cell.longLabel.text = self.user[indexPath.row].userLongitude
    cell.latLabel.text = self.user[indexPath.row].userLatitude
    //I believe that you need to go to userCell and define these variables


    return cell
}

我认为您需要进行两项更改:

替换

self.tableview.reloadData()

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

由于您正在对 UI 进行更改,因此您应该 运行 在主线程上进行更改。

其次,我认为您应该删除 viewDidDisappear 中的观察者,因此从上面的代码中删除这一行 ref.removeAllObservers()

我能看到的另一个问题是这段代码:

 if let fullName = value["full name"] as? String, let imagePath = value["urlToImage"] as? String
                        ,let userLongitude = value["long"] as? String, let userLatitude = value["lat"] as? String
                        //for some reason adding this makes it impossible to get code
                        //userLongitude and userLatitude cause no users to show up


                    {
                        userToShow.fullName = fullName
                        userToShow.imagePath = imagePath
                        userToShow.userID = uid
                        userToShow.userLongitude = userLongitude
                        userToShow.userLatitude = userLatitude

                        self.user.append(userToShow)
                    }

此处如果任何值不可用,则根本不会填充用户,而是您应该一一填充它们:

userToShow.fullName = value["full name"] as? String
userToShow.imagePath = value["urlToImage"] as? String
...

1) 在 View First Load 中调用 retrieve user 函数作为其 ObserveSingleEvent,因此它会 Observe 并且 运行 只是一次,而不是像 observe

这样的处理程序

2) 从 StoryBoard 中删除 tableView 的委托和数据源以及重新加载的时间 table 像这样在此处提供

self.tableview.datasource = self
self.tableview.delegate = self
self.tableview.reloadData()

3) Handler 运行 异步所以重新加载 table Handler 执行后查看所以使用 dispatch Queue

DispatchQueue.main.async {
   self.tableview.datasource = self
   self.tableview.delegate = self
   self.tableview.reloadData()
}

请注意,如果在执行以下步骤后数据仍然为空,table

中不会弹出任何用户

1) 尝试打印添加到 usersArray 的用户值

2) 当您重新加载 tableView in numbersOfItemInSection 时,还会在此处打印数组以检查数据是否正在传递给 tableView。

3) 如果数组的值在 numbersOfItemInSection 中,则检查用于单元格的用户单元格 class,重新连接插座