【问题标题】:Updating a UITableView on Firebase Data Change在 Firebase 数据更改上更新 UITableView
【发布时间】:2017-11-29 04:42:59
【问题描述】:

所以我试图让表格单元格在任何其他单元格发生更改时自动刷新。我希望我的代码可以一举找到表格数组中的对象,更改值并刷新表格视图,但是由于某种原因,即使我的数组更新了,表格也拒绝立即更新。要查看我的新值,我必须重新启动应用程序或重新打开视图。

这就是我目前正在做的事情:

usersRef.queryOrdered(byChild "contactList/(currentUID!)").queryEqual(toValue: true).observe(.childChanged, with: { (snapshot) in
if let friends = snapshot.value as? [String: AnyObject] {

            //Making a new updated contact

            let name = friends["name"] as! String
            print(name)
            let edit = Contact()
            edit.name = name;
            edit.isSafe = friends["isSafe"] as! Bool
            edit.email = friends["email"] as! String

            //My manager object has a custom array objects called contacts. 
            self.manager.index(withName: name, contact: edit)
            self.tableView.reloadData();
        } else {
            print("Failed to Convert")
        }
    })

}

ContactManager 中的index(withName: String, contact: Contact) 方法:

//This method goes through the contactList to check names to see which contact has a specific name equal to "name", then it uses the "contact" to update that specific child.

    public mutating func index(withName: String, contact: Contact) {
    for iterator in 0 ..< contactList.count {
        if (contactList[iterator].name == withName) {
            contactList[iterator] = contact
        }
    }
}

【问题讨论】:

  • self.tableView.reloadData(); 应该在主线程中调用

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

这是一个常见问题。您在后台线程中调用self.tableView.reloadData()。所有 UI 更改都需要在主线程上进行。像这样。

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

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多