Swift: How to fix infinite loop when adding a value to a Firebase variable

后端 未结 2 613
情话喂你
情话喂你 2021-01-16 12:13

I have this piece of code in my program that should allow a user to tap on a message and the score should increase by one:

super.collectionView(collectionVie         


        
2条回答
  •  没有蜡笔的小新
    2021-01-16 12:48

    You have set up an observer that should observe any value changes in the "messages"-tree and all of its children nodes.

    This means that whenever you update the score, you'll also receive information on this in your observer - and your code will run again. To fix this so your score only will be set once you have to change your observer to only fetch changes once:

    rootRef.child("messages").observeSingleEventOfType(.Value, withBlock:
    

    Read more here: https://firebase.google.com/docs/database/ios/retrieve-data

    I would also recommend you to take a look at the structure your database-section: https://firebase.google.com/docs/database/ios/structure-data

提交回复
热议问题