1

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
D. Finna
  • 467
  • 2
  • 7
  • 19

3 Answers3

2

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

Community
  • 1
  • 1
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • I updated to your suggestion, but when I try to use the code it said: `fatal error: unexpectedly found nil while unwrapping an Optional value` – D. Finna Sep 13 '16 at 15:39
  • I am using `.updateChildValues`, but I will try the other one I just need to figure out how to make it fit to my code. – D. Finna Sep 14 '16 at 08:44
  • is there anywhere, where we can chat? I have something that I hope you will help me with, since I cannot get the functionality of my like button to work the way I want to – D. Finna Sep 14 '16 at 10:09
1

updateChildValues accepts [AnyHashable:Any] dictionary:

self.dbRef.child("feed-items/\(dataPathen)/likesForPost")
    .updateChildValues(["likesForPost": "7"])
mixel
  • 25,177
  • 13
  • 126
  • 165
  • I tried your code but it gives me this error: `fatal error: unexpectedly found nil while unwrapping an Optional value`... I also tried removing the likesForPost in the child ref but without luck – D. Finna Sep 13 '16 at 15:41
  • @D.Finna May be it's because `dataPathen` is `Optional`? – mixel Sep 13 '16 at 15:42
  • Could you update your question with full example including dbRef and dataPathen declaration? – mixel Sep 14 '16 at 08:49
  • Thank you for your help mixel, but I figured out using Dravidian's code. – D. Finna Sep 14 '16 at 09:14
0
  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"])
Ashvini
  • 342
  • 3
  • 11