Writing the data

In Firebase, we have four methods available to write a data into the Firebase database:

set( ) Write or replace data to a defined path, like messages/tickets/<uid>.
update( ) Update to specific children of node without replacing the other child nodes. We can also use the update method to update the data into multiple locations.
push( ) To add a list of data in the database, we can use the push() method; it generates a unique ID every time when it calls, such as helpdesk/tickets/<unique-user-id>/<unique-ticket-id>.
transaction( ) We can use this method when we are working with complex data that can be corrupted by concurrent updates such as incremental counters.

 

Now, let's take a look at how data is structured in our helpdesk  application:

{
"tickets": {
"-L4L1BLYiU-UQdE6lKA_": {
"comments": "Need extra 4GB RAM in my system"
"date": "Fri Feb 02 2018 15:51:10 GMT+0530 (India Standa..."
"department": "IT"
"email": "[email protected]"
"issueType": "Hardware Request"
"status": "progress"
},
"-L4K01hUSDzPXTIXY9oU": {
"comments": "Need extra 4GB RAM in my system"
"date": "Fri Feb 02 2018 15:51:10 GMT+0530 (India Standa..."
"department": "IT"
"email": "[email protected]"
"issueType": "Hardware Request"
"status": "progress"
}
}
}

Now, let's take an example of the preceding data structure and use the set() method to store the data with autoincrementing integer:

{
"tickets": {
"0": {
"comments": "Need extra 4GB RAM in my system"
"date": "Fri Feb 02 2018 15:51:10 GMT+0530 (India Standa..."
"department": "IT"
"email": "[email protected]"
"issueType": "Hardware Request"
"status": "progress"
},
"1": {
"comments": "Need extra 4GB RAM in my system"
"date": "Fri Feb 02 2018 15:51:10 GMT+0530 (India Standa..."
"department": "IT"
"email": "[email protected]"
"issueType": "Hardware Request"
"status": "progress"
}
}
}

Now if you see the preceding data structure, new tickets will be stored as /tickets/1. This will work if only a single user were adding tickets, but in our application, many users can add tickets at the same time. If two employees write to /tickets/2 simultaneously, then one of the tickets will be deleted by the other. So this will not be recommended practices and always recommend to use the push() method to generate a unique ID (refer to the preceding data structure) when you are working with list of data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset