-
Notifications
You must be signed in to change notification settings - Fork 16
K.2 Init Firestore
You can initialize Firestore by calling Firestore.init();
As soon as you initialized the firebaseFirestore.ane in your project, you will be ready to read and write data to your the Firestore.
NOTE: Make sure you have already initialized the Firebase core before trying any other Firebase child ANEs.
Cloud Firestore stores data in Documents, which are stored in Collections. Cloud Firestore creates collections and documents implicitly the first time you add data to the document. You do not need to explicitly create collections or documents.
Create a new collection and a document using the following example code.
// Create a new user with a first and last name
var user:Object = {
first: "Ada",
last: "Lovelace",
born: 1815
};
// Add a new document with a generated ID
var collection:CollectionReference = Firestore.collection("users");
collection.addEventListener(FirestoreEvents.COLLECTION_ADD_SUCCESS, onCollectionAddSuccess);
collection.addEventListener(FirestoreEvents.COLLECTION_ADD_FAILURE, onCollectionAddFailure);
collection.add(user);
function onCollectionAddSuccess(e:FirestoreEvents):void
{
var c:CollectionReference = e.target as CollectionReference;
c.removeEventListener(FirestoreEvents.COLLECTION_ADD_SUCCESS, onCollectionAddSuccess);
c.removeEventListener(FirestoreEvents.COLLECTION_ADD_FAILURE, onCollectionAddFailure);
trace("DocumentSnapshot added with ID: " + e.documentReference.documentId);
}
function onCollectionAddFailure(e:FirestoreEvents):void
{
var c:CollectionReference = e.target as CollectionReference;
c.removeEventListener(FirestoreEvents.COLLECTION_ADD_SUCCESS, onCollectionAddSuccess);
c.removeEventListener(FirestoreEvents.COLLECTION_ADD_FAILURE, onCollectionAddFailure);
trace("Error adding document: " + e.msg);
}
Now add another document to the users collection. Notice that this document includes a key-value pair (middle name) that does not appear in the first document. Documents in a collection can contain different sets of information.
// Create a new user with a first, middle, and last name
var user:Object = {
first: "Alan",
middle: "Mathison",
last: "Turring",
born: 1912
};
// Add a new document with a generated ID
var collection:CollectionReference = Firestore.collection("users");
collection.addEventListener(FirestoreEvents.COLLECTION_ADD_SUCCESS, onCollectionAddSuccess);
collection.addEventListener(FirestoreEvents.COLLECTION_ADD_FAILURE, onCollectionAddFailure);
collection.add(user);
To quickly verify that you've added data to Cloud Firestore, use the data viewer in the Firebase console.
You can also use the "read" method to retrieve the entire collection.
var collection:CollectionReference = Firestore.collection("users");
collection.addEventListener(FirestoreEvents.QUERY_GET_SUCCESS, onCollectionReadSuccess);
collection.addEventListener(FirestoreEvents.QUERY_GET_FAILURE, onCollectionReadFailure);
collection.read();
function onCollectionReadSuccess(e:FirestoreEvents):void
{
var c:CollectionReference = e.target as CollectionReference;
c.removeEventListener(FirestoreEvents.QUERY_GET_SUCCESS, onCollectionReadSuccess);
c.removeEventListener(FirestoreEvents.QUERY_GET_FAILURE, onCollectionReadFailure);
for(var i:int=0; i < e.querySnapshot.documents.length; i++)
{
var doc:DocumentSnapshot = e.querySnapshot.documents[i];
trace(doc.snapshotId + " => " + JSON.stringify(doc.data));
}
}
function onCollectionReadFailure(e:FirestoreEvents):void
{
var c:CollectionReference = e.target as CollectionReference;
c.removeEventListener(FirestoreEvents.QUERY_GET_SUCCESS, onCollectionReadSuccess);
c.removeEventListener(FirestoreEvents.QUERY_GET_FAILURE, onCollectionReadFailure);
trace("Error getting documents: " + e.msg);
}
- If you're using the Web, Android, or iOS SDK, use Firebase Authentication and Cloud Firestore Security Rules to secure your data in Cloud Firestore.
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