Swift无法识别Firebase用户

时间:2017-11-07 03:04:32

标签: ios swift firebase firebase-realtime-database core-location

我非常擅长编码并尝试了解如何从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
}

2 个答案:

答案 0 :(得分:1)

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

替换

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 :(得分:0)

1)在View First Load中调用检索用户函数作为其ObserveSingleEvent,这样它将只观察并运行一次而不像处理程序那样观察

2)从StoryBoard中删除tableView的委托和数据源以及重新加载表的时间在这里提供

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

3)处理程序异步运行所以重新加载表执行处理程序后查看所以使用调度队列

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

请注意,如果以下步骤后数据仍为空,则表

中不会弹出任何用户

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

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

3)如果数组的值为numbersOfItemInSection,则检查用于单元格的用户单元类,重新连接出口

相关问题