-
Notifications
You must be signed in to change notification settings - Fork 16
B.2 Init Realtime DB
You can initialize the realtime database by calling DB.init();
As soon as you initialized the firebaseDatabase.ane in your project, you will be ready to read and write data to your Firebase database.
NOTE: Make sure you have already initialized the Firebase core before trying any other Firebase child ANEs.
initialize an instance of your database and reference the location you want to write to.
import com.myflashlab.air.extensions.firebase.db.*;
// Write a message to the database
DB.init();
var myRef:DBReference = DB.getReference("message");
myRef.setValue("Hello, World!");
You can save a range of data types to the database this way, including ActionScript Arrays or Objects. When you save an object the responses from any getters will be saved as children of this location.
To make your app data update in realtime, you should add a DBEvents.VALUE_CHANGED
listener to the reference you just created.
This event is triggered once when the listener is attached and again every time the data changes, including the children.
// Read from the database
myRef.addEventListener(DBEvents.VALUE_CHANGED, onDataChange);
myRef.addEventListener(DBEvents.VALUE_CHANGE_FAILED, onCancelled);
function onDataChange(e:DBEvents):void
{
// This method is called once with the initial value and again
// whenever data at this location is updated.
if (e.dataSnapshot.exists)
{
if (e.dataSnapshot.value is String) trace("onValueChanged String value = " + e.dataSnapshot.value);
else if (e.dataSnapshot.value is Number) trace("onValueChanged Number value = " + e.dataSnapshot.value);
else if (e.dataSnapshot.value is Boolean) trace("onValueChanged Boolean value = " + e.dataSnapshot.value);
else if (e.dataSnapshot.value is Array) trace("onValueChanged Array value = " + JSON.stringify(e.dataSnapshot.value));
else trace("onValueChanged Object value = " + JSON.stringify(e.dataSnapshot.value));
}
}
function onCancelled(e:DBEvents):void
{
trace("Failed to read value: " + e.msg);
}
Before launching your app, we recommend walking through our launch checklist to make sure your app is ready to go!
Enjoy building Air apps – With ♥ from MyFlashLabs Team
Introduction to Firebase ANEs collection for Adobe Air apps
Get Started with Firebase Core in AIR
- Prerequisites
- Add Firebase to your app
- Add the Firebase SDK
- Init Firebase Core
- Available ANEs
- Managing Firebase iid
Get Started with Authentication
- Add Authentication
- Init Authentication
- Manage Users
- Phone Number
- Custom Auth
- Anonymous Auth
- State in Email Actions
- Email Link Authentication
Get Started with FCM + OneSignal
- Add FCM ANE
- Init FCM ANE
- Send Your 1st Message
- Send Msg to Topics
- Understanding FCM Messages
- init OneSignal
- Add Firestore
- Init Firestore
- Add Data
- Transactions & Batches
- Delete Data
- Manage the Console
- Get Data
- Get Realtime Updates
- Simple and Compound
- Order and Limit Data
- Paginate Data
- Manage Indexes
- Secure Data
- Offline Data
- Where to Go From Here
Get Started with Realtime Database
- Add Realtime Database
- Init Realtime Database
- Structure Your Database
- Save Data
- Retrieve Data
- Enable Offline Capabilities
Get Started with Remote Config
- Add Storage ANE
- Init Storage ANE
- Upload Files to Storage
- Download Files to Air
- Use File Metadata
- Delete Files