Based on your subsequent comments, here are a few things that might help. Forgive the long answer, but just try to help reduce and simplify the amount of code you write.
In general, disassociating keys from data is a good model. The reason for this is: what if the user changes his name? They marry with a different surname, decide that they like to call Jim instead of James, etc. If you use your name as a key, it is not mutable, and in any other place that you access, it will also depend on it. If you make the name a child node, you can change it at any time without affecting other data.
The first step is to change the structure to something like this
{ "mentors" : { "uid_0" : { "user_name": "Ash Dreyer", "current_status" : "IN", "num_of_logins" : 0, "total_hours" : 0 }, "uid_1" : { "user_name": "Donald Pinckney", "current_status" : "OUT", "num_of_logins" : 0, "total_hours" : 0 },
uid_0, uid_1, etc. are created by Firebase when the user is created and can be obtained from authData.uid. This is a great piece of data to use in a key for each user that you store with your / mentors (or / users) node.
When a user logs in, you have several options: if you do not need their data, you can simply update the current_status node. You know the specific name of the node, since this is their auth.uid.
let ref = /mentors node let thisUser = ref.childByAppendingPath(auth.uid) let thisUsersStatusRef = thisUser.childByAppendingPath("current_status") thisUsersStatusRef.setValue("In")
and do the same for the timestamp.
If you need to display their name in the user interface, just read their details through watchSingleEvent and update the current status and timestamp.
let ref = /mentors node let thisUser = ref.childByAppendingPath(auth.uid) ref.observeSingleEventOfType(.Value, withBlock: { snapshot
When the user subscribes, all you have to do is calculate in a few hours, set current_status to exit and record the timestamp
thisUsersStatusRef.setValue("Out") thisUsersHoursRef.setValue(hours) thisUsersTimeStamp.setValue(timestamp)
The big picture here is that if you read the user data when you log in, you have all the information ahead to calculate the hours, etc., since it can be stored in a variable, so when they log out, do calculation and write. This way, it essentially only hits Firebaes twice (once when they log in and once when they log out).
The last thing that every user should observe is / mentors node for the events childAdded, childChanged or childRemoved (NOT Value). With these observers, when a user is added, edited or changed, all users are notified with this single data node and can update their interface accordingly.