Introduction:
Welcome to the documentation for Firebase Firestore! Firestore is a flexible NoSQL document database from Firebase that simplifies storing, syncing, and querying data for your applications. In this documentation, we will cover all the essential features and functionalities of Firestore.
Installation:
To start using Firestore in your iOS project, you need to follow these easy steps:
- Open your Swift project in Xcode.
- Ensure that you have CocoaPods installed on your machine.
- Add Firestore to your project’s
Podfile
by adding the following line:
“`ruby
pod ‘FirebaseFirestore’
“`
Save the file and run the following command in your project’s directory:
“`bash
pod install
“`
Getting Started:
Before using Firestore, you need to set up Firebase in your project. Follow these steps to get started:
- Go to the Firebase Console.
- Create a new project or select an existing one.
- Enable Firestore by navigating to the Firestore Database section in the console.
- Follow the provided instructions to add the necessary GoogleService-Info.plist file to your project.
Firestore Basics:
Firestore organizes data into collections and documents. Here’s a quick overview of some basics:
- Collections: A collection is a group of documents.
- Documents: A document is a set of key-value pairs.
- Fields: Fields are the individual key-value pairs within a document.
Firestore CRUD Operations:
Create:
To create a new document in a collection, you can use the following code:
“`swift
let docData: [String: Any] = [
“title”: “Document Title”,
“content”: “This is the content of the document”,
// Add more fields as needed
]
let docRef = Firestore.firestore().collection(“collectionName”).document()
docRef.setData(docData) { (error) in
if let error = error {
print(“Error adding document: \(error)”)
} else {
print(“Document added!”)
}
}
“`
Read:
To read data from Firestore, use the following code:
“`swift
Firestore.firestore().collection(“collectionName”).getDocuments { (snapshot, error) in
if let error = error {
print(“Error getting documents: \(error)”)
} else {
for document in snapshot!.documents {
print(“\(document.documentID) => \(document.data())”)
}
}
}
“`
Update:
To update a document in Firestore, use the following code:
“`swift
let docRef = Firestore.firestore().collection(“collectionName”).document(“documentID”)
docRef.updateData([
“title”: “Updated Title”,
“content”: “Updated content”
]) { (error) in
if let error = error {
print(“Error updating document: \(error)”)
} else {
print(“Document updated!”)
}
}
“`
Delete:
To delete a document from Firestore, use the following code:
“`swift
let docRef = Firestore.firestore().collection(“collectionName”).document(“documentID”)
docRef.delete { (error) in
if let error = error {
print(“Error deleting document: \(error)”)
} else {
print(“Document deleted!”)
}
}
“`
Conclusion:
Congratulations! You have learned the basics of Firebase Firestore and how to perform CRUD operations in your iOS application. Explore the official Firestore documentation for more advanced features and functionalities.