Aprove answer

If there is 1000 songs and 1000 people voting, creating one semaphore will have 999 people waiting even if they are all voting on different songs. Correct?

If you have 1-million people, you'd have 1-million semaphores to synchronize your database? Think about scaling..

You need to use a Transaction operation for your database. See here: https://firebase.google.com/docs/database/ios/read-and-write#save_data_as_transactions

The above example seems to be the same thing you're attempting to do (voting with stars and incremental counters).

You cannot synchronize a remote database with a sempahore on the client-side anyway.. Unless I'm mistaken and your DB is local. In any case, a Transaction operation is the way to go.

Credit Goes to: stackoverflow.com

Related question with same questions but different answers