【问题标题】:Swift Not Recognizing Firebase UsersSwift 无法识别 Firebase 用户
【发布时间】:2017-11-07 03:04:32
【问题描述】:

我对编码非常陌生,并且正在尝试学习如何从 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()之前添加此语句print(self.user)并检查它打印的内容?
  • 它没有打印任何东西。
  • 你能展示一下你的firebase数据库的结构吗?

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


【解决方案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
...

【讨论】:

  • @BeccaD 请检查更新的答案,看看它是否对您有帮助
  • @BeccaD 暂时评论if 语句,并尝试逐个设置值
  • 我最终不得不删除 firebase 中的所有用户才能使其正常工作。感谢您的洞察力。
【解决方案2】:

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 中,则检查用于 cell 的用户单元类,在那里重新连接插座

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    相关资源
    最近更新 更多