/firebase-tutorials

How to update data in Realtime Database?

Learn how to update data in Firebase Realtime Database with step-by-step instructions, code examples for Web, Android, iOS, and security tips.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to update data in Realtime Database?

 

Step 1: Setting Up the Firebase Project

 

First, ensure you have a Firebase project set up. If you haven’t, go to the Firebase Console and create a new project. Follow the on-screen instructions to set up your Firebase account and project.

 

Step 2: Setting Up Realtime Database

 

After setting up your Firebase project, navigate to the "Realtime Database" section in the Firebase Console and create a new database. Choose the appropriate location and start in "Test mode" if you're not familiar with setting up rules.

 

Step 3: Adding Firebase to Your Application

 

For Web:

Add Firebase SDK to your project. Include the Firebase library in your HTML file. Add the following to your <head> section:

<!-- Include Firebase SDK -->
<script src="https://www.gstatic.com/firebasejs/9.x.x/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.x.x/firebase-database.js"></script>

For Android:

Add Firebase to your Android app by including the google-services.json file in your app directory. This file can be downloaded from the Firebase Console under Project Settings > Your apps.

For iOS:

Add Firebase to your iOS app by including the GoogleService-Info.plist file in your Xcode project. This file can also be downloaded from the Firebase Console under Project Settings > Your apps.

 

Step 4: Initialize Firebase

 

Below is the initialization code for different platforms. Use the one that corresponds to your app's platform.

For Web:

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: "YOUR_API_KEY",
  authDomain: "YOUR_AUTH_DOMAIN",
  databaseURL: "YOUR_DATABASE_URL",
  projectId: "YOUR_PROJECT_ID",
  storageBucket: "YOUR_STORAGE_BUCKET",
  messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
  appId: "YOUR_APP_ID"
};

// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig);
const database = firebase.database();

For Android (Inside onCreate method):

// Initialize Firebase
FirebaseDatabase database = FirebaseDatabase.getInstance();

For iOS (Inside application:didFinishLaunchingWithOptions method):

// Initialize Firebase
[FIRApp configure];

 

Step 5: Updating Data in Realtime Database

 

For Web:

To update data in Firebase's Realtime Database, use the update() method:

// Reference to specific node
var updates = {};
updates['/path/to/node'] = { key: 'newValue' };
database.ref().update(updates);

For Android:

In Android, you update data similarly using the updateChildren() method:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("/path/to/node");
Map<String, Object> updates = new HashMap<>();
updates.put("key", "newValue");
ref.updateChildren(updates);

For iOS:

For iOS, use the updateChildValues method:

FIRDatabaseReference *ref = [[FIRDatabase database] referenceWithPath:@"path/to/node"];
NSDictionary *updates = @{@"key": @"newValue"};
[ref updateChildValues:updates];

 

Step 6: Testing Your Updates

 

Ensure your app is running with the Realtime Database and try updating the data. Check Firebase Console to see if the updates are reflected in the database.

 

Step 7: Setting Database Rules for Security

 

After testing, make sure you set proper rules by going to the "Rules" tab under Realtime Database in Firebase Console. A simple rule configuration might look like:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

These rules will ensure only authenticated users can read or write data. Modify the rules to fit your specific requirements.

 

By following these steps, you can successfully update data within the Firebase Realtime Database across different platforms.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022