Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Google Cloud Firestore

Overview

Google Cloud Firestore is a scalable NoSQL database for mobile, web, and server development from Firebase and Google Cloud Platform. It allows developers to store and sync data for client- and server-side development.

Note: Firestore is designed to scale automatically and provides real-time updates to data.

Setup

To set up Firestore:

  1. Go to the Firebase Console.
  2. Create a new project or select an existing project.
  3. In the left sidebar, select "Firestore Database".
  4. Click "Create Database".
  5. Choose a starting mode (test mode or locked mode).
  6. Click "Next" and follow the instructions to complete the setup.

Data Structure

Firestore is structured as a collection of documents. Each document can contain various fields and supports complex data types such as arrays, maps, and nested structures.

Important: Each document is identified by a unique document ID within a collection.

        // Example of a Firestore document structure
        {
            "name": "John Doe",
            "age": 30,
            "isActive": true,
            "address": {
                "city": "Los Angeles",
                "state": "CA"
            },
            "skills": ["Java", "Swift", "Python"]
        }
        

CRUD Operations

Firestore allows you to perform Create, Read, Update, and Delete (CRUD) operations. Below are examples for each operation.

Create


        // Swift code to add a document
        let db = Firestore.firestore()
        db.collection("users").document("userID").setData([
            "name": "John Doe",
            "age": 30,
            "isActive": true
        ]) { err in
            if let err = err {
                print("Error adding document: \(err)")
            } else {
                print("Document added successfully.")
            }
        }
        

Read


        // Swift code to read a document
        db.collection("users").document("userID").getDocument { (document, error) in
            if let document = document, document.exists {
                print("Document data: \(document.data())")
            } else {
                print("Document does not exist.")
            }
        }
        

Update


        // Swift code to update a document
        db.collection("users").document("userID").updateData([
            "age": 31
        ]) { err in
            if let err = err {
                print("Error updating document: \(err)")
            } else {
                print("Document updated successfully.")
            }
        }
        

Delete


        // Swift code to delete a document
        db.collection("users").document("userID").delete() { err in
            if let err = err {
                print("Error removing document: \(err)")
            } else {
                print("Document successfully removed!")
            }
        }
        

Best Practices

  • Use batched writes for multiple updates to reduce network costs.
  • Structure your data in a way that optimally fits your query patterns.
  • Limit the number of reads/writes to avoid exceeding Firestore quotas.
  • Utilize indexing to enhance query performance.
  • Implement security rules to protect your data.

FAQ

What is Firestore pricing?

Firestore pricing is based on the number of reads, writes, and deletes performed on the database, along with storage costs. You can refer to the Firebase Pricing page for detailed information.

Can I use Firestore offline?

Yes, Firestore has built-in support for offline data persistence on mobile and web clients, allowing your app to function even when offline.

Is Firestore suitable for large-scale applications?

Absolutely! Firestore is designed to scale horizontally and can handle large volumes of data and users, making it suitable for enterprise-grade applications.