D. Finna
D. Finna

Reputation: 467

Update string with firebase swift

I am trying to update a string with firebase swift but I am getting an error that I do not know how to get rid of.

I have this code part that is getting an error:

self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues("likesForPost": "7") 

The error I am getting is expected "," seperator just before the :. I am using dbRef in another code part so I know i works and the dataPathen is being printed just before the above code part, so that is working too.

Can anyone help me with this bug?

Upvotes: 1

Views: 407

Answers (3)

Ashvini
Ashvini

Reputation: 352

  1. Whenever updating values at any reference in Firebase Database, you need to pass a dictionary parameter for updateChildValues method as [AnyHashable: Any] for your path reference. So just update your code of line as below:
  2. self.dbRef.child("feed-items/(dataPathen)/likesForPost").updateChildValues("likesForPost": "7")
  3. Also if you need to update more than 1 key-value pairs then you can pass those key-value pairs inside dictionary by seperating using comma as below:
  4. self.dbRef.child("feed-items/(dataPathen)/likesForPost").updateChildValues(["likesForPost": "7", "otherKey": "OtherKeyValue"])

Upvotes: 0

Dravidian
Dravidian

Reputation: 9945

Just change

self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues("likesForPost": "7") 

To

self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues(["likesForPost": "7"]) 

And if you are only looking for incrementing a particular value at a specific node you might wanna check my answer's :- https://stackoverflow.com/a/39465788/6297658, https://stackoverflow.com/a/39471374/6297658

PS Prefer runTransactionBlock: to update properties like likeForPosts as there might be a moment when two users try to like same post at the same moment (Highly Unlikely, but still a possibility...),using updateChildValues might end up just updating like only from one user. But runTransactionBlock: keep firing until the changes of that thread have been committed to the node

Upvotes: 2

mixel
mixel

Reputation: 25866

updateChildValues accepts [AnyHashable:Any] dictionary:

self.dbRef.child("feed-items/\(dataPathen)/likesForPost")
    .updateChildValues(["likesForPost": "7"])

Upvotes: 1

Related Questions