This quickstart shows you how to set up Cloud Firestore, add data, then view the data you just added in the Firebase console.
Create a Cloud Firestore project
Open the Firebase Console and create a new project.
In the Database section, click Try Firestore Beta.
Click Enable.
When you create a Cloud Firestore project, it also enables the API in the Cloud API Manager.
Set up your development environment
Add the required dependencies and client libraries to your app.
- Follow the instructions to add Firebase to your iOS app.
- Add the Cloud Firestore pod to your
Podfile
pod 'Firebase/Core'
pod 'Firebase/Firestore' - Save the file and run
pod install
.
Initialize Cloud Firestore
Initialize an instance of Cloud Firestore:
// Use Firebase library to configure APIs
[ FIRApp configure ];
FIRFirestore * defaultFirestore = [ FIRFirestore firestore ];
Add data
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.
__block FIRDocumentReference * ref =
[[ self . db collectionWithPath :@ "users" ] addDocumentWithData :@{
@ "first" : @ "Ada" ,
@ "last" : @ "Lovelace" ,
@ "born" : @ 1815
} completion :^( NSError * _Nullable error ) {
if ( error != nil ) {
NSLog (@ "Error adding document: %@" , error );
} else {
NSLog (@ "Document added with ID: %@" , ref . documentID );
}
}];
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.
__block FIRDocumentReference * ref =
[[ self . db collectionWithPath :@ "users" ] addDocumentWithData :@{
@ "first" : @ "Alan" ,
@ "middle" : @ "Mathison" ,
@ "last" : @ "Turing" ,
@ "born" : @ 1912
} completion :^( NSError * _Nullable error ) {
if ( error != nil ) {
NSLog (@ "Error adding document: %@" , error );
} else {
NSLog (@ "Document added with ID: %@" , ref . documentID );
}
}];
Read data
To quickly verify that you've added data to Cloud Firestore, use the data viewer in the Firebase console.
You can also use the "get" method to retrieve the entire collection.
getDocumentsWithCompletion :^( FIRQuerySnapshot * _Nullable snapshot ,
NSError * _Nullable error ) {
if ( error != nil ) {
NSLog (@ "Error getting documents: %@" , error );
} else {
for ( FIRDocumentSnapshot * document in snapshot . documents ) {
NSLog (@ "%@ => %@" , document . documentID , document . data );
}
}
}];
Secure your data
- 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.
- If you're using one of the server SDKs, use Identity and Access Management (IAM) to secure your data in Cloud Firestore.
Watch a video tutorial
For detailed guidance on getting started with the Cloud Firestore mobile and web client libraries, watch one of the following video tutorials:
You can find more videos in the Firebase YouTube channel.
Next steps
Deepen your knowledge with the following topics:
- Codelabs — Learn to use Cloud Firestore in a real app by following the codelab for Android, iOS, or Web.
- Data model — Learn more about how data is structured in Cloud Firestore, including hierarchical data and subcollections.
- Add data — Learn more about creating and updating data in Cloud Firestore.
- Get data — Learn more about how to retrieve data.
- Perform simple and compound queries — Learn how to run simple and compound queries.
- Order and limit queries Learn how to order and limit the data returned by your queries.