我正在构建一个非常简单的应用程序,人们点击按钮,它会记录他们使用firebase数据库点击按钮(签入)的时间。我没有问题存储签到的人的数据,然后显示签到所有适当信息的人的数据。我已经包含了一张图片来显示我已经在TableViewController中显示的内容。
我不想只显示已签入的用户我还想显示尚未登记的用户。
我现在将分享我的firebase数据库如何设置以存储每个用户的照片。
以下是整个数据库的视图,其中显示已使用名称和时间戳签入的用户和用户
我还将包含来自我的UITableViewController的代码,这样每个人都可以看到我只显示已经签到的人。
import Firebase
struct checkInStruct {
let userName : String!
let hour : String!
let minutes : String!
}
class checkInDaysTableViewController: UITableViewController {
//Variables
var lastChildNameSegue = ""
var posts = [checkInStruct]()
let user = FIRAuth.auth()?.currentUser
override func viewDidLoad() {
super.viewDidLoad()
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) , at: 0)
self.tableView.reloadData()
})
}
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 minuteInt = Int(minute!)
if(minuteInt! >= 0 && minuteInt! <= 9){
let minuteWithZero = "0\(minuteInt!)"
minute = minuteWithZero
}
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = "\(userName!)"
let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = "Checked In At \(hour!):\(minute!)"
return cell!
}
}
我需要做什么才能从所有用户加载数据并突出显示尚未签入的红色用户?
答案 0 :(得分:0)
我认为你应该像这样设计数据库。
root -> users
"users": [
"{id_user_A}": {
"name": A,
"checkedin": [
"{id_checkedin_1}": {
...
},
"{id_checkedin_2}": {
...
}
]
},
"{id_user_B}": {
"name": B,
"checkedin": []
}
]
获取所有用户,使用checkedin
属性检查每个用户,以了解签入的用户。
答案 1 :(得分:0)
你必须改变你的JSON树结构
Users : {
userID_1 : {
userName : John Joe,
checkInID : UNIQUE_ID_1;
},
userID_2 : {
userName : Meghan,
checkInID : UNIQUE_ID_2;
} ,
userID_3 : {
userName : Maria,
checkInID : UNIQUE_ID_3;
}
},
CheckIn_List : {
UNIQUE_ID_1 : true; // This id has checked in
UNIQUE_ID_2 : true; // This id has checked in
UNIQUE_ID_3 : false; // This id has not yet checked in..
},
CheckIn_User_Info : {
UNIQUE_ID_1 : {
userName : John Joe,
time : //time;
}
UNIQUE_ID_2 : {
userName : Meghan,
time : //time;
}
UNIQUE_ID_3 : {
userName : Maria,
time : //time;
}
}
您想要遵循的程序: -
如果您要对应用的每个用户进行身份验证,那么您已经拥有了user_ID,那么您需要担心的是可以用作Firebase server Timestamp
的签入ID。
var checkinID = FIRServerValue.timestamp()
如果您未对用户进行身份验证;跳过第一部分,您仍然需要 checkinID
将这些附加到安全规则
"Users" : {
".write" : "auth != null && auth.uid === $uid", // Will allow only the user to make any change within its node and no-one else
".read": "auth != null && auth.uid === $uid"
},
"CheckIn_User_Info" : {
".write" : "auth != null",
".read" : "auth != null"
},
"CheckIn_List": {
".write" : "auth != null",
".read" : "auth != null"
}
我为checkIn id使用了不同的唯一ID,因为将来你可能想要添加一些你可能想要隐藏其他用户的用户细节,这样就解决了这个问题