RJH RJH - 10 months ago 88
Swift Question

Saving FIRServerValue.timestamp() - Hashable protocol error

The following code successfully saves data ("test":FIRServerValue.timestamp()) to my Firebase database:

let updateData = [ "[agent]:\(self.messageText)": (FIRServerValue.timestamp()) ] as [String : Any]

let update = self.dbConnector.child("messages/\(key)")
update.updateChildValues(updateData) { (error, dbRef) in
if (error == nil) {
// ok
} else {
// failed

However, I want the timestamp to be the key and the "test" to be the value, eg. FIRServerValue.timestamp():"test"

When I switch them around to this:

let updateData = [(FIRServerValue.timestamp()) : "test"] as [Any : String]

I get the error:

Type 'Any' does not conform to protocol 'Hashable'

Having read a few other questions around Hashable protocols I am confused whether this is possible?

Answer Source

FIRServerValue.timestamp() is a command that is send to your Firebase Server, which then creates an timeStamp value and then stores it.

If you print you get a null dictionary, How a dictionary works is that your key value pair of dictionary cannot have a null key, Since you dont recieve a key of FIRServerValue.timestamp() in your front end, its still null in the updateValues.

So you cannot define an empty key