Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 30, 2019 12:00 am

How to Use Firebase Firestore in an iOS App

Final product image
What You'll Be Creating

In this tutorial, you'll learn how to manage data in a Firebase Firestore database.

For some background on Firebase, and instructions on how to get your Firestore database set up, please read my earlier Introduction to Firebase post.

Data, Documents, and Collections

Working with Firestore, data, documents and collections are the key concepts which we need to understand.

Data

Data can be any of the following types: 


  • String

  • Number

  • Boolean

  • Map

  • Array

  • Null

  • Timestamp

  • Geopoint

  • Reference

Documents

Documents contain data items in a set of key-value pairs. Firebase Firestore is optimised for storing large collections of documents. A document is a lightweight record with fields which map to values. Each document is identified by a name.

For example, we could describe a Country document as follows:

Here Country is the document which contains two fields: Name and Capital.


Collections

Collections contain sets of documents. Documents live in collections, which are simply containers for documents. For example, you could have a Countries collection to contain your various countries, each represented by a document:

In short data is part of a document, which belongs to a collection.

We'll learn more about data, documents and collections by building a sample app.

Creating a New Firebase App

To start, you'll need to create a new Xcode project and set it up in the Firebase Firestore portal. 

First, create a project in Xcode and name it ToDoApp. Then, create a cooresponding project in Firebase and also name it ToDoApp.

Create a project in Firebase and name it ToDoApp

Next, register your app with a bundle identifier.

Register the app

Firebase will create a config file for you to include in your project. Download the config file and add it to the root of your Xcode project as shown.

Download the GoogleService config file

Here's a screenshot of my project folder structure with the GoogleService config file highlighted.

The GoogleService config file in the project folder

Next, you need to install the CocoaPods for Firebase. CocoaPods make it easy to install and manage external dependencies such as third-party libraries or frameworks in your Xcode projects. If you don't have a Podfile already, follow the instructions to create one. Then add the Firebase core services to you Podfile and run pod install.

Initialize and add the required pods

Next, the Firebase setup wizard will give you some code to add to your AppDelegate. Copy the highlighted lines below into your AppDelegate file.

Add initialization code to your AppDelegate file

When you've completed all these steps, the project setup screen in Firebase will look like this:

Firebase project setup screen awaiting verification

To complete the installation, you need to get your app to communicate to the Firebase server. Simply run your Xcode project and go back to the project setup screen in Firebase and you should see something like this:

Successfully verified installation

Handling Data in a Firebase App

Now that we are done with the initial set up, let's start with handling data in our app.

In this section of the tutorial we'll create an app to edit, delete and read data about tasks. But before we start adding or creating tasks, we need to think about fields we would require for a task. Since this is a simple app we'll stick with the following single field:


  • task_details: text describing the task

We also need a collection, which we'll name tasks which will contain these Task documents. This is how the structure will look:

To represent above data, let's create a simple task model class in TaskModel.swift with the code below:

Reading Tasks From the Collection

We will be displaying our tasks in a simple table view.

First, let's create a ToDoListViewController.swift class. Then add ToDoListViewController in Main.storyboard and connect it to ToDoListViewController.swift. Add a UITableView to ToDoListViewController in the storyboard and implement delegates. 

Now we shall create ListService.swift to hold the code to retrieve task data. Start by importing FirebaseFirestore. Then create a Firestore instance withFirestore.firestore(). ListService.swift should look as follows:

Next we'll add the completeList method in the ListService class. We'll get all the data and documents from the tasks collection by using the getDocuments() method from Firebase. This will either return data or an error.

In this code, querySnapshot contains the returned data. It will be nil if there is no collection with the name tasks

Here is the rest of the completeList function: it retrieves the task data and calls the completion callback with any retrieved data.

Let's try it out! Use the code below to call completeList from ToDoListViewController

There should be no errors, but querySnapshot will be nil since we do not have any documents in our collection yet. In the next section, we'll add some data and then try calling completeList method again

Adding Tasks to the Collection

Let's create an addToList method in ListService().

We'll use the passed in task description to create a taskData instance and add it to the tasks collection. To do this we will use the addDocument method from Firestore Firebase. 

If there is no error we'll print the document id and return true in our completion block. task_id will be the document id. For now we'll just send the empty string, but eventually we'll update the task_id with the correct document id.

Let's add some test data by calling addToList from ViewController. After running the below code go back to the console and you'll see that an entry was. Now running getAllTasks() should return true and a tasks count of 1.

Of course, in a real app, you'd want to create a user interface for adding tasks!

Deleting a Task Document in Firestore

Finally, let's see how we can delete a document in Firestore. Go back to the Firebase Firestore console and make a note of the document id value for "Buy Groceries". We will be deleting the document based on the document id.

Now, add a deleteFromList method to our ListService as shown below.

Let's call deleteFromList from ToDoListViewController. We'll pass it the document id of "Buy Groceries" which we copied from the Firebase console. 

Running the above code, you should see status as true. If we go to the Firebase Firestore console, we should see that the "Buy Groceries" task has been deleted.


Conclusion

With that, you've learned how to create, read and delete data from a Firebase Firestore database. In this tutorial, we built an app to manage tasks using Xcode and Firebase. Hopefully, this has given you some new skills which you’ll be able to put into practice in your upcoming projects. 

To make this app more functional, you could implement table view delegate methods to display the tasks in your database, a UITextField to add new tasks, and a swipe gesture  to delete tasks. Try it out!

If you have any questions, let me know in the comments below.


Original Link: https://code.tutsplus.com/tutorials/how-to-use-firebase-firestore-in-an-ios-app--cms-32216

Share this article:    Share on Facebook
View Full Article

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code