所以我正在提出一个朋友请求,并且在获取这些请求时出现了错误。
此类不适用于键xxx的键值编码
通过发送请求可以正常工作(these are the values from sending a request):
let fromId = (Auth.auth().currentUser!.uid)
let toId = self.otherUser?["uid"] as! String
let senderFriendRef = databaseRef.child("friendship").child(fromId)
senderFriendRef.updateChildValues([toId : 0])
let receiverFriendRef = databaseRef.child("friendship").child(toId)
receiverFriendRef.updateChildValues([fromId : 1])
这是获取请求的代码。我玩过if (snapshot.value as? Int == 1)
,看来这可能导致崩溃,因为在出现错误之前,我无法将其强制转换为Int
:
if let loggedInUser = Auth.auth().currentUser?.uid {
Database.database().reference().child("friendship").child(loggedInUser).observe(.childAdded) { (snapshot) in
if (snapshot.value as? Int == 1) {
let requestId = snapshot.key
Database.database().reference().child("users").child(requestId).observeSingleEvent(of: .value, with: { (snapshot) in
if let dict = snapshot.value as? [String : AnyObject] {
let person = Person()//model
person.setValuesForKeys(dict)
person.id = requestId
self.friendRequest.append(person)
print(self.friendRequest)
DispatchQueue.global().async {
DispatchQueue.main.async {
self.collectionView.reloadData()
//ending brackets