Swift 和 Firebase 查询数据库
Swift and Firebase querying the database
我正在尝试创建一个用户参与的聊天表视图。我正在关注他们网站上的 firebase 教程,他们说要轻松获取用户所属的聊天室列表,以创建 child 并将房间名称添加到 child。
所以我的结构看起来像这样
Users
UNIQUE KEY
nickname: "name"
rooms
name: true
Room
etc etc
所以在我的 cellForRow 中我使用了这个代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
firebase.child("users").child(fUID).observeSingleEvent(of: .value, with: { snapshot in
for user in snapshot.children.allObjects as! [FIRDataSnapshot]{
self.names = (user.value?["participating"] as? String)!
}
})
cell.textLabel?.text = self.names
cell.detailTextLabel?.text = "test"
return cell
}
我收到一个错误,当我 PO 名称时它出现一个空字符串
有人可以帮助我了解问题所在以及如何解决吗?谢谢
编辑 1
我已经获得部分工作的代码
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let ref = firebase.child("users").child(fUID).child("participating")
ref.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.value)
var dict = [String: Bool]()
dict = snapshot.value as! Dictionary
for (key, _) in dict {
self.names = key
print(self.names)
}
self.rooms.append(self.names)
self.tableView.reloadData()
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.rooms.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.rooms[indexPath.row]
cell.detailTextLabel?.text = "test"
return cell
}
现在的问题是 firebase 中有 2 个项目...它只显示其中一个
您使用的代码就是挑战。这是一个简化版本:
let usersRef = firebase.child("users")
let thisUser = usersRef.childByAppendingPath(fUID)
let thisUsersRooms = thisUser.childByAppendingPath("rooms")
thisUsersRooms.observeSingleEventOfType(.Value, withBlock: { snapshot in
if ( snapshot.value is NSNull ) {
print("not found")
} else {
for child in snapshot.children {
let roomName = child.key as String
print(roomName) //prints each room name
self.roomsArray.append(roomName)
}
self.myRoomsTableView.reloadData()
}
})
话虽如此,应该从 viewDidLoad 中调用这段代码来填充一个数组,然后随着 tableView 的刷新,应该从该数组中提取数据来填充您的 cellView。
我正在尝试创建一个用户参与的聊天表视图。我正在关注他们网站上的 firebase 教程,他们说要轻松获取用户所属的聊天室列表,以创建 child 并将房间名称添加到 child。
所以我的结构看起来像这样
Users
UNIQUE KEY
nickname: "name"
rooms
name: true
Room
etc etc
所以在我的 cellForRow 中我使用了这个代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
firebase.child("users").child(fUID).observeSingleEvent(of: .value, with: { snapshot in
for user in snapshot.children.allObjects as! [FIRDataSnapshot]{
self.names = (user.value?["participating"] as? String)!
}
})
cell.textLabel?.text = self.names
cell.detailTextLabel?.text = "test"
return cell
}
我收到一个错误,当我 PO 名称时它出现一个空字符串
有人可以帮助我了解问题所在以及如何解决吗?谢谢
编辑 1
我已经获得部分工作的代码
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let ref = firebase.child("users").child(fUID).child("participating")
ref.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.value)
var dict = [String: Bool]()
dict = snapshot.value as! Dictionary
for (key, _) in dict {
self.names = key
print(self.names)
}
self.rooms.append(self.names)
self.tableView.reloadData()
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.rooms.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.rooms[indexPath.row]
cell.detailTextLabel?.text = "test"
return cell
}
现在的问题是 firebase 中有 2 个项目...它只显示其中一个
您使用的代码就是挑战。这是一个简化版本:
let usersRef = firebase.child("users")
let thisUser = usersRef.childByAppendingPath(fUID)
let thisUsersRooms = thisUser.childByAppendingPath("rooms")
thisUsersRooms.observeSingleEventOfType(.Value, withBlock: { snapshot in
if ( snapshot.value is NSNull ) {
print("not found")
} else {
for child in snapshot.children {
let roomName = child.key as String
print(roomName) //prints each room name
self.roomsArray.append(roomName)
}
self.myRoomsTableView.reloadData()
}
})
话虽如此,应该从 viewDidLoad 中调用这段代码来填充一个数组,然后随着 tableView 的刷新,应该从该数组中提取数据来填充您的 cellView。