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

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
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.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.