我正在使用firebase swift 3.我是firebase的新手。因此,我构建了一个应用程序,用户可以注册并发布一个事件,其他用户可以查看事件,如果他们想参加,则单击“参加”按钮。成功我可以实现所有这一切,但我的问题是当用户点击出席按钮时,它将保存为peoplewhoattend child下的用户uid。现在我想看看那些参加活动的人。我想我应该将用户uid链接到用户个人资料,但我不能这样做。我已经尝试了很多并找到了一些类似的问题,但没有任何关系。这是我的代码。提前感谢每一个积极的评论和帮助
@IBAction func btnAttend(_ sender: Any) {
self.btnattend.isEnabled = true
let dbRef = Database.database().reference()
let uid = Auth.auth().currentUser!.uid
let storageRef = Storage.storage().reference(forURL: "gs://event-f39cc.appspot.com")
let key = dbRef.child("posts").childByAutoId().key
let imageRef = storageRef.child("posts").child(uid).child("\(key).jpg")
let data = UIImageJPEGRepresentation(self.imgEvent.image!, 0.5)
dbRef.child("users").child("posts").child(self.EventId!).observeSingleEvent(of: .value, with: { (snapshot) in
let updateattends = ["peopleWhoattend/\(key)" : Auth.auth().currentUser!.uid , "UserEmail" : Auth.auth().currentUser?.email , "Username" : Auth.auth().currentUser?.displayName] as [String: Any]
dbRef.child("posts").child(self.EventId!).updateChildValues(updateattends, withCompletionBlock: {(error, reff) in
if error == nil{
dbRef.child("posts").child(self.EventId!).observeSingleEvent(of: .value, with: {(snap) in
if let properties = snap.value as? [String: AnyObject] {
if let Attend = properties["peopleWhoattend"] as? [String: AnyObject]{
let count = Attend.count
self.lblattend.text = "\(count)"
let update = ["Attend" : count]
dbRef.child("posts").child(self.EventId!).updateChildValues(update)
self.btnattend.isHidden = false
// self.unlikeBtn.isHidden = false
//self.unlikeBtn.isEnabled = true
}
}
})
}
})
})
dbRef.removeAllObservers()
}
<pre>
{
"posts" : {
"-KrbDH19ERe9Os5RrEin" : {
"Attend" : 10,
"Category" : "Film & Media",
"Event date End" : "15/08/2017 10:47 PM",
"Event date Start" : "15/08/2017 10:47 PM",
"EventId" : "-KrbDH19ERe9Os5RrEin",
"EventTitle" : "hhh",
"Fee" : "6",
"Location" : {
"latitude" : 23.61349209999999,
"longitude" : 58.5434909,
"place name" : "Ruwi St, Muscat, Oman"
},
"User Email" : "salwa@hotmail.com",
"author" : "abeer",
"commentsId" : "-KrbDH19ERe9Os5RrEio",
"description" : "Event Description : dsdss",
"likes" : 3,
"pathToImage" : "https://firebasestorage.googleapis.com/v0/b/event-f39cc.appspot.com/o/events%2FhjAPqucu2jh3sY6v9n9nRM5Fuz73%2F-KrbDH19ERe9Os5RrEin.jpg?alt=media&token=795f2555-f36d-49d7-9bdd-e521fcd14c16",
"peopleWhoLike" : {
"-KrdnWbWtQ7zmkVwN7MZ" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-Krdnymu9a_zP81JFDzm" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KsZb-WL9dptWI7dHWDf" : "3IHHOBRaVyR3ldK4hGtcGoY7lox2"
},
"peopleWhoattend" : {
"-KrbDLO6ZIpIxt4lTax0" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KrbDMri-KrHDrvCAHS4" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KrbE3OppAyB_zI5XacN" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KrbE3bgAZgvLs9J2KJr" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KrbE3vSo1Z6EqOAO4SJ" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KrbE4H_kpwgsMRlRCt6" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KrbE4Xa2CP_KXC8dQbi" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KtL2Egv2slRRk8f_rF1" : "H5kCOhIQFMbxQlN5dzZYC8aptxR2",
"-KtL2TXGz5JkCd1vA3Z2" : "H5kCOhIQFMbxQlN5dzZYC8aptxR2",
"-KtL2TtWdIFn3I1zvMAD" : "H5kCOhIQFMbxQlN5dzZYC8aptxR2"
},
"userId" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"username" : "Salwa"
},
"-KrdlVlme6Jc6eVbNBTa" : {
"Attend" : 4,
"Category" : "Science & Technology",
"Event date End" : "20/08/2017 10:40 AM",
"Event date Start" : "16/08/2017 10:40 AM",
"EventId" : "-KrdlVlme6Jc6eVbNBTa",
"EventTitle" : "hhhh",
"Fee" : "6666",
"Location" : {
"latitude" : 23.6006718,
"longitude" : 58.55438340000001,
"place name" : "(Great Valley (Alkbir Wadi, Muscat, Oman"
},
"User Email" : "salwa@hotmail.com",
"author" : "abeer",
"commentsId" : "-KrdlVlme6Jc6eVbNBTb",
"description" : "Event Description : ttttt",
"likes" : 5,
"pathToImage" : "https://firebasestorage.googleapis.com/v0/b/event-f39cc.appspot.com/o/events%2FhjAPqucu2jh3sY6v9n9nRM5Fuz73%2F-KrdlVlme6Jc6eVbNBTa.jpg?alt=media&token=4471c063-5432-474e-82b3-030f44ce231f",
"peopleWhoLike" : {
"-Krdlhj9fflCIEguJlPZ" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KrdlhtXEdxuUMRfYJsR" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-Krdlhz1914-GsfDsK21" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KsZDW38ka7dB5NtfrNm" : "Rufida",
"-KsnWCXJNFlZJT5qbh8J" : "FWTfyvXqsXXdI9sy3GihCx8o5a63"
},
"peopleWhoattend" : {
"-Krdlbv9aKWlYNGa1K_d" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-Krdldu17azum7ZH5u-E" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KrdlgBey4dkBLY8FBBK" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"-KtL2V0WEYwRLCLpGvvW" : "H5kCOhIQFMbxQlN5dzZYC8aptxR2"
},
"user name" : "rufida@hotmail.com",
"userId" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"username" : "Salwa"
},
"users" : {
"3IHHOBRaVyR3ldK4hGtcGoY7lox2" : {
"name" : "Rufida",
"uid" : "3IHHOBRaVyR3ldK4hGtcGoY7lox2",
"urlToImage" : "https://firebasestorage.googleapis.com/v0/b/event-f39cc.appspot.com/o/users%2F3IHHOBRaVyR3ldK4hGtcGoY7lox2.jpg?alt=media&token=1c3ee6f2-f688-40b4-b1d6-69f90bf19bb1"
},
"FWTfyvXqsXXdI9sy3GihCx8o5a63" : {
"Email" : "Ola@hotmail.com",
"name" : "Ola",
"uid" : "FWTfyvXqsXXdI9sy3GihCx8o5a63",
"urlToImage" : "https://firebasestorage.googleapis.com/v0/b/event-f39cc.appspot.com/o/users%2FFWTfyvXqsXXdI9sy3GihCx8o5a63.jpg?alt=media&token=289d9b9c-65c1-4ac0-83e4-7f688925f927"
},
"H5kCOhIQFMbxQlN5dzZYC8aptxR2" : {
"Email" : "Salwa@hotmail.com",
"name" : "Salwa",
"uid" : "H5kCOhIQFMbxQlN5dzZYC8aptxR2",
"urlToImage" : "https://firebasestorage.googleapis.com/v0/b/event-f39cc.appspot.com/o/users%2FH5kCOhIQFMbxQlN5dzZYC8aptxR2.jpg?alt=media&token=8f676259-7240-43d2-8735-0896ec9ad090"
},
"hjAPqucu2jh3sY6v9n9nRM5Fuz73" : {
"name" : "abeer",
"uid" : "hjAPqucu2jh3sY6v9n9nRM5Fuz73",
"urlToImage" : "https://firebasestorage.googleapis.com/v0/b/event-f39cc.appspot.com/o/users%2FhjAPqucu2jh3sY6v9n9nRM5Fuz73.jpg?alt=media&token=2c343a4c-c6ad-4dcf-bd0d-10e9a7a635cb"
}
}
}
import UIKit
import Firebase
import FirebaseStorage
class AttendeesCollectionViewController: UIViewController , UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var posts = [Events]()
@IBOutlet weak var CVAttendee: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor(red: 0, green: 128.0/255.0, blue: 64.0/255.0, alpha: 1.0)
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
// Do any additional setup after loading the view.
fetchEvents()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height: CGFloat = view.frame.size.height
let width: CGFloat = view.frame.size.width
return CGSize(width: CGFloat(width * 0.95), height: CGFloat(height * 0.75))
}
override func viewDidAppear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.posts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UserCell", for: indexPath) as! AttendeeCollectionViewCell
//Creating the cell
cell.lblFirstName.text = self.posts[indexPath.row].FirstName
cell.lblLastName.text = self.posts[indexPath.row].LastName
cell.lblNumberAttendee.text = self.posts[indexPath.row].Attend
if let imagePath = self.posts[indexPath.row].userPic{
cell.imgUser.downloadImage(from: imagePath)
}
else{
cell.imgUser.image = UIImage(named: "abstract-user-flat-4")
}
cell.EventId = self.posts[indexPath.row].EventId
cell.lblEmail.text = self.posts[indexPath.row].Email
cell.lblPhoneNum.text = self.posts[indexPath.row].PhoneNumber
return cell
}
func fetchEvents(){
let dbRef = Database.database().reference()
dbRef.child("Attendee").queryOrdered(byChild: "EventId").observeSingleEvent(of: .value, with: { (snapshot) in
let postData = snapshot.value as! [String: AnyObject]
for(_ ,value ) in postData{
let event = Events()
if
let FirstName = value["UserFirstName"] as? String,
let LastName = value["UserLastName"] as? String,
let Email = value["Email"] as? String,
let PhoneNumber = value["PhoneNumber"] as? String,
let attendingID = value["attendingID"] as? String, // let NumberAttend = value["Event date End"] as? String,
let pathToImage = value["pathToImage"] as? String,
let EventId = value["EventId"] as? String,
let userId = value["userId"] as? String {
event.FirstName = FirstName
event.LastName = LastName
event.Email = Email
event.PhoneNumber = PhoneNumber
event.pathToImage = pathToImage
// event.dateStart = NumberAttend
event.EventId = EventId
event.userId = userId
event.attendingID = attendingID
dbRef.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
let users = snap.value as! [String: AnyObject]
for(_ ,userVal) in users {
if let uid = userVal["uid"] as? String{
if uid == userId{
if let img = userVal["urlToImage"] as? String{
event.userPic = img
}
}
}
}
})
dbRef.child("Attendee").observe(.value, with: { (snap) in
// post.commentCount = Int(arc4random_uniform(UInt32(snap.childrenCount)))
let comments = snap.value as! NSDictionary
for key in (comments.allKeys) {
if key as! String == attendingID{
let attendee = comments.value(forKey: attendingID) as! NSDictionary
if let attending = attendee.value(forKey: "Attendees"){
let attending = attending as? NSDictionary
// event.commentCount = messages?.count
}
else{
// event.commentCount = 0
}
self.CVAttendee.reloadData()
}
}
})
self.posts.append(event)
}
}
})
dbRef.removeAllObservers()
}
}
import UIKit
import Firebase
class Events: NSObject {
var author: String!
var Email: String!
var likes: Int!
var Attend : String!
var commentId: String!
var attendingID : String!
var pathToImage: String!
var userId: String!
var userPic: String!
var EventId: String!
var dateStart: String!
var dateEnd: String!
var desc: String!
var Category: String!
var Fee: String!
var EventTitle: String!
var commentCount: Int!
var SponsorPakage : String!
var locationAddress: NSDictionary!
var peopleWhoLike: [String] = [String]()
var peopleWhoattend: [String] = [String]()
var FirstName: String!
var LastName: String!
var PhoneNumber : String!
var go: goingStatus!
}