What is Cloud Firestore Database

Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. Like Firebase Realtime Database, it keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web so you can build responsive apps that work regardless of network latency or Internet connectivity.

Cloud Firestore also offers seamless integration with other Firebase and Google Cloud Platform products, including Cloud Functions. Firestore is currently in Beta at the time of writing this post, so it’s not recommended to use it in your main project, but still fully functional.

Source: Firebase Docs

What is Special about Cloud Firestore Database

Cloud Firestore is very much like the Firebase Realtime Database, but with many amazing improvements! With the Realtime Database you save your data in a nested tree, but with Cloud Firestore you save data in collections and documents with all the usual fields thrown in too which you can declare, including, Strings, Booleans, Objects, Arrays, and more! Using Collections and Documents makes for really powerful queries on your data!

First Steps we take – Install Cocoapods

We will be using Cocoapods to add Cloud firestore to our Xcode Project. If you don’t have Cocoapods installed you can it is quite simple to do, but you can find more information about it More Information About Cocoapods

Simply put, you need to open Terminal and run the following command:

sudo gem install cocoapods

Create A Pod File

Now we have Cocoapods installed, we can now create a podfile in our Xcode Project. If you have not already created an Xcode project, please makes sure you do this now, I won’t go into how to do that here as it is out of the scope of this tutorial.

In Terminal cd into your project root then run the following command:

pod init

This command creates a pod file in the root of your project. Now simple locate this podfile and double click it to open it up in a text or code editor as we will be adding some content to it. please do not close your Terminal window as we will be using this in a moment.

Import Firestore in our Podfile

When you open your podfile you need to add 3 Imports just underneath where it says use_frameworks!. The 3 imports we need are

  • pod ‘Firebase/Core’
  • pod ‘Firebase/Auth’
  • pod ‘Firebase/Firestore’

Here is what your podfile should look like now with your imports under the use_frameworks! section

pod 'Firebase/Core'
pod 'Firebase/Auth'
pod 'Firebase/Firestore'

Install our new Imports

Now you can close the podfile and save the changes, then simply go back to your terminal window. If you closed it earlier, please make sure you cd into the root of your Xcode project before continuing. Now all you have to do is run the following command:

pod install

Simple as that! now you have the dependencies you need to use firebase in your project.

Import the GoogleService-Info.plist File

To connect your Firebase App with your Xcode App you need to get the GoogleService-Info.plist file from Firebase, which is very simple to do. First of all login to your Firebase Account, if you have not already created a project, do this now, if you have already got a project in Firebase then simply click into the project, then click the cog at the top left of the screen, near the Firebase logo to open the Project Settings Page.

Cloud Firestore tutorial

Now all you need to do is click the big circle that says IOS

Cloud Firestore tutorial

You will need to follow the steps shown on the screen, but when complete it will download a GoogleService-Info.plist file to your computer. This need to be added to your Xcode Project.

When you go back into your Xcode project directory you will see that you have 2 Xcode project files.

  • yourappname.xcodeproj
  • yourappname.xcworkspace

It is important that you now use only the yourappname.xcworkspace file when developing your app. Don’t worry, this is still your Xcode Project, it has been generated when we added the pod files to your project so has all the dependencies included in it. So go ahead and open this file up.

Now all you have to do is drag the GoogleService-Info.plist into the root of your project so it sits alongside all the other files that make up your app in Xcode.

Edit our AppDelegate.swift File

In your Xcode project open up your AppDelegate.swift file. we need to add a few things here. First we need to import a couple of things at the top of the AppDelegate file so go ahead and add these 2 lines at the top of the file, just underneath, import UIKit

import Firebase
import FirebaseAuth

Now we need to add one more thing to the AppDelegate didFinishLaunchingWithOptions Function, simply add FirebaseApp.configure() like so:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true

Now when you create any new swift file for your view controllers and you want to use the full features of Firebase including Firestore and User Authentication, simply import these 3 lines at the top of the swift file to allow you to use all the methods from Firebase

import Firebase
import FirebaseAuth
import FirebaseFirestore

Congratulations, you did it!

That’s all you need to do, your app is now ready to read and write data to your Firestore Database, and also ready to authenticate users and it all works directly with your Firebase project!