首先,我在Firebase中存储了2组数据。我将在照片中包含两组。
第一张照片显示了我保留所有用户个人资料的位置。
当有人签入应用程序时,我会将他们的用户名和登记入住时间存储在其他地方。
在上面的照片中,您将看到当前有2位用户已登记" 12-3-2017 Block 2"
我存储数据没问题。
但是我在显示数据方面遇到了问题。
我希望能够创建一个UITableView,它显示所有已签入的用户以及他们签入的时间,而在同一列表中显示所有尚未签入的用户。
import UIKit
import Firebase
struct checkInStruct {
let userName : String!
let hour : String!
let minutes : String!
let userNameFromAllUsers : String!
}
class checkInDaysTableViewController: UITableViewController {
//Variables
var lastChildNameSegue = ""
var posts = [checkInStruct]()
let user = FIRAuth.auth()?.currentUser
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "\(lastChildNameSegue)"
print("Name of Child After Segue -> \(lastChildNameSegue)")
loadCheckedInUsers()
loadAllUsers()
print(posts)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let userName = posts[indexPath.row].userName
let hour = posts[indexPath.row].hour
var minute = posts[indexPath.row].minutes
let userNameOfAllUsers = posts[indexPath.row].userNameFromAllUsers
let label1 = cell?.viewWithTag(1) as! UILabel
let label2 = cell?.viewWithTag(2) as! UILabel
if(hour == nil || hour == ""){
//Users isnt signed in
label1.text = "\(userName!)"
label2.text = "User Not checked in"
}else{
label1.text = "\(userName!)"
label2.text = "\(hour!):\(minute!)"
}
return cell!
}
func loadCheckedInUsers (){
let databaseRef = FIRDatabase.database().reference()
let userID = user?.uid
databaseRef.child("users").child("\(userID!)").child("checkins").child("\(lastChildNameSegue)").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
let userNameSnapValue = snapshot.value as? NSDictionary
let userName = userNameSnapValue?["userName"] as? String
let hourSnapValue = snapshot.value as? NSDictionary
let hour = hourSnapValue?["hour"] as? String
let minutesValue = snapshot.value as? NSDictionary
let minutes = minutesValue?["minutes"] as? String
print(userName!)
print(hour!)
print(minutes!)
self.posts.insert(checkInStruct(userName: userName, hour: hour, minutes: minutes, userNameFromAllUsers: userName) , at: 0)
self.tableView.reloadData()
})
}
func loadAllUsers(){
let databaseRef = FIRDatabase.database().reference()
let userID = user?.uid
databaseRef.child("users").child("\(userID!)").child("employeesorstudents").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
let userNameSnapValue = snapshot.value as? NSDictionary
let userName = userNameSnapValue?["userName"] as? String
let hour = ""
let minutes = ""
print(userName!)
self.posts.insert(checkInStruct(userName: userName, hour: hour, minutes: minutes, userNameFromAllUsers: userName) , at: 0)
self.tableView.reloadData()
})
}
}
如您所见,我将数据存储在一个结构中。
但是,在应用程序中,它显示所有用户的数据库中的用户名和已签入的用户。
在屏幕截图中,您将看到" Brandon Happy"有一个登记入住时间然后他再次出现说他从未登记过。如何删除结构中的重复数据?
答案 0 :(得分:1)
一切看起来都不错但是如果已经存在,你应该做的就是不向结构中添加数据,所以你需要做的是修复你的loadAllUsers()函数。
func loadAllUsers(){
let databaseRef = FIRDatabase.database().reference()
let userID = user?.uid
databaseRef.child("users").child("\(userID!)").child("employeesorstudents").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
let userNameSnapValue = snapshot.value as? NSDictionary
let userName = userNameSnapValue?["userName"] as? String
let hour = ""
let minutes = ""
print(userName!)
databaseRef.child("users").child("\(userID!)").child("checkins").child("\(self.lastChildNameSegue)").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("\(userName!)"){
print("User already checked in")
}else{
print("user has not yet checked in")
self.posts.insert(checkInStruct(userName: userName, hour: hour, minutes: minutes, userNameFromAllUsers: userName) , at: 0)
self.tableView.reloadData()
}
})
})
}
在if语句中,它将确定用户是否已经签入,如果尚未签入,则将其添加到结构中。