Setup Cloud Firestore Database 2021 (SwiftUI, Swift Package Manager)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Last week I released a Firestore Setup tutorial, but little did I know the Firebase SDK is now compatible with Swift Package Manager. If you didn't know, the Swift Package Manager helps you add manage third party code libraries with your Xcode project, among other things. But the nice thing about Swift Package Manager is that it's integrated right into Xcode, so it's a smooth and easy process to use. This video is a rerecording of that Firestore Setup tutorial using Swift Package Manager this time. If you're just interested in the Swift Package Manager section, check in the description below for the time stamp and jump straight there. Otherwise we're gonna start from the beginning. Firestore is a free remote database solution for your mobile app using. It allows you to do cool things like letting your users create accounts, saving user generated data, and syncing data across devices just to name a few. This video is a guide on how to start using Firestore Database for your iOS app, you'll learn how to set up a new Firebase project with a new Firestore database instance, you'll learn how to add the Firebase SDK to your Xcode project and you'll learn how to use Swift Package Manager. By the end of this tutorial, you'll be ready to harness the power of Firestore database. Hey, code crew, my name is Chris and I've been teaching Beginners how to code and build app since 2013. If you're brand new to app development and you're just starting out on a journey, check out our 14 day Beginner challenge. It's the best place to start. In fact, we have a community full of people who are on the same journey taking the exact same challenge. So definitely check that out with that said, let's dive into today's topic. First, let's take a look at the bigger picture here's how your app is going to interact with the Firestore database. First of all, Fireface is a platform that you can go and create a project on. When you do that, you'll get your own Firebase project, which also has a Firestore database. Your Firestore database is in the cloud so that your data is accessible from anywhere as long as you have the address to the database. Those details are stored in a configuration file called the GoogleService Info Plist file. And yes, in case you didn't know, Firebase is owned by Google. Next, you have your Xcode project, which you're planning to use with your Firestore database. So in order for your Xcode project to know where to read data from and to write data to it needs to know the address of your database and that's where that configuration file comes in. You drag and drop that configuration file into your Xcode project, and now it has the details to connect to your Firestore database. Alright, so now your Xcode project knows where your database is, but how do you actually write the Swift code to interact with it, such as reading data from it, writing data to it. Well, Firebase has made that easy for us by writing all the code already for us and giving us a bunch of classes that we can use. We just have to supply the data that we want to put into the database or maybe the query if we're trying to retrieve data from it. Oh, and we also have to handle any potential errors as well. In part two of this series, you'll learn to do all of that for now. We're interested in adding these Firebase classes to our Xcode project so we can use them to interact with our Firestore database. These classes are collectively known as the Firebase SDK. There are a couple of ways to add the Firebase SDK to our Xcode project, but in this tutorial we'll be using the Swift Package Manager, which helps us add and manage thirdparty libraries for Xcode, so that's at a high level how Firebase works, Firebase is known as a backend as a service, and that means that a lot of the infrastructure and nitty gritty details of database management and setup are handled for you. Things like load balancing and database replication are all invisible to you, and all you need to care about is your data and your app. Now, the other nice thing about Firebase is that it's got a bunch of other services that you can use in addition to Firestore, things like analytics, messaging, authentication and more. So if one of these features catches your eye and you are interested in learning more about it and you'd like me to create a tutorial for it, drop me a comment below. The last thing I want to mention is that we can use all of these things for free until we hit bigger numbers. All right, let's dive into how to set up Firestore database for our iOS app. Alright, so let's create our brand new Xcode project. Let's click on create a new Xcode project. Under is let's choose App and I'm going to call this Firestore Demo SPM for Swift Package Manager. Take note of this bundle Identifier. We'll get a chance to copy and paste this later because we're going to need this to set up our Firebase project. Make sure your interface is Swift UI life cycle is Swift UI app and your language is Swift and you can leave core data and tests unchecked. Let's choose where we want to save this. I'm going to create this on my desktop. All right. And there's our brand new Xcode project. Now, if you click on this root node here in the File Navigator, you're going to get a chance to copy this, which I'm going to do right now. Next we're going to go over to Firebase Google. Com and set up our Firebase project using this bundle Identifier. So here we are at Firebase Google. Com and you need a Google account to use this for free. If you have one, log in to it in the upper right corner. I'm already logged in. So I'm just going to click go to console. That's where we're going to see all of your Firebase project. So you can see I have a bunch of demo ones. I'm going to start a brand new one just for the sake of this demo. So click Add project and you can name it anything you'd like. Let's call it Firestore SPM demo and click continue. Now, Google Analytics would be great for a project that you are planning to ship into the App store. But for this demo, I am going to turn it off because we don't need it. So let's go ahead and create the project. Now it's going to take a minute or two for it to create your project, but we've just sped that up in post production. So now we are going to set up and configure our Firebase project so that we can point our Xcode project to this instance. Click on iOS right here. And this is where we are going to paste in our project bundle ID. I had copied it from Xcode, so I'm just going to paste it here. But just to remind you, if we look into our Xcode project, you click this root node right here. This is what you are looking for. So copy that. Paste it in there. You can go ahead and fill in some of these optional things if you need to, I'm going to register the app. The next thing that's going to do is generate a configuration file for us to download and add to our Xcode project. So let's go ahead and download this GoogleService info plist and that's going to contain the details to point to this Firestore instance. Now I actually already have one on my desktop. So that's why it's named it like this. Why don't I go ahead and delete that first? And so it's got to be named this Google Service info. Otherwise it's not going to work. It's going to it's not going to be able to successfully find this configuration file. So let's save it on my desktop and we are going to click and drag that file into our Xcode project. So let's go back to our Xcode project right here. And depending on where you saved it, go ahead and drag that Google Service infoplist into your project. Now I'm going to put it right there under or above the Info P list and just make sure that you have copy items that's needed is checked so that it's going to make a copy of it inside your project folder. This is also very important. Add to targets. Make sure this is checked. So there it is. This contains all of the information my project needs to know in order to point to this Firebase project. So let's go ahead and click next. Now we're going to add the Firebase SDK into our Xcode project, and remember, these are the code libraries that our project can use to call different functions and methods to interact with the Firebase platform. Now what you see here are the instructions for Cocoa Pods, but we're not going to do that in this tutorial. We're going to use Swift Package Manager. So go ahead and click this link. It's going to give you some documentation for it, but all you really need to know is this URL. So go ahead and click this to copy it, because this is going to be that reference to the Firebase SDK. This is helpful if you don't know how to use this with Package Manager, it just tells you where to find this menu option inside Xcode, but we're going to do that right now together. Go to Xcode and then under file Swift packages add package dependency and you're going to paste that URL in there and click next. That's going to take a couple of seconds. Recommends you to leave it as is up to the next major version, and you click next and it's going to fetch all of the available modules because Firebase contains a lot of different features and you might not need all of that code. So in this instance where interested in using Firestore database, so that's the only module we're gonna add. So we're gonna look at that in a second and give it some time to go. But I want to go back to this documentation for just a second because I do want to note that if you want to use this method to install Firebase SDK, you're going to need Xcode 12.5 or higher. Right. So just a note. Let's go back. Okay. So it's ready now. These are all of the different different components you can add. We're going to scroll down and look for Fire store now. For some reason in this version of Xcode, at least I can't really expand these different sections, but I can tell you that what I believe this one to be is beta, right. So we probably don't want to install that. This is the only thing we need. Firebase Firestore it's unfortunate that I can't really expand any of these things to see. Okay, so we're going to make sure that that is checked and we're going to click finish and grabbing it right here. You're going to see. In addition to Firestore, it's also grabbed all of these other dependencies that it needs to work. And just like that, we've added the Firebase SDK, at least for the Firestore bits that we need. What we're going to do now is just build the project to make sure that it runs. I'm going to switch similar to iphone Twelve. I'm going to press Command B to build the project and make sure that it happen successfully. Now this actually might take quite a while, depending on your machine and how fast it is. It could take up to a couple of minutes, but hopefully it results in success. Alright. So we're looking good. Now let's go back to the setup steps here and we're going to click next because we've already just added the Firebase SDK, and the next thing we need to do is add two lines of code, import Firebase and Firebase app dot configure. Now in this code app, it right here. It's not meant for a Swift UI project because it has the class app delegate, but in Swift I if you create a new Swift I project, you might find that you don't have that. That's okay because you can still do that in this file right here. So instead of the app delegate file, you do that right here and you put import Firebase, and then you might also find that you don't have this method right here called application did finish launching with options. So what we're going to do instead is inside this opening curly bracket right here. We're going to create an init method which will automatically be run when your app gets initialized, and we're going to put that line of code in there. Let's click that. Copy that to clipboard and put it right there. And just to double check, you can set a Breakpoint right there and you can run your project and make sure that it hits that Breakpoint. If you're new to break points. This is just a way for you to stop the execution of code at that specific line of code, and then you can do various things like debug. What's going on? Great for troubleshooting. So here it's hit our Breakpoint, meaning that it is about to run this line of code. So I'm just going to click continue execution and our program runs, and we've just connected our Xcode project to our Firebase project right here. Let's click on next and then continue to console. So now our setup is complete. Let us set up our Firestore database. If we go over to the left hand side here Firestore database. We're simply going to hit Create database. Now it's going to ask us to start in production or test mode. We're going to go ahead and start in test mode, which essentially will allow anyone who has the data contained in the Google service Info file a reference to this instance. They're going to be able to write to our Firestore database. Now that's definitely not what you want in production, but for demos and for testing and things like that. This is great. It also puts a time stamp on it, so it's automatically going to close itself off at a certain time because it it doesn't want you to indefinitely leave public databases, readable and writable to anyone indefinitely. So it's going to put a time stamp on there. And you can change that if you need more time or anything like that. But we're going to start in test mode. Choose the closest location to you. You can change this after the fact. Sothere you have it. We have our brand new empty Firestore database ready to be used and we have our Xcode project with the Firebase SDK added to it. It's pointing to this Firebase instance and it's ready to interact with this database. Alright, so you set up your new Firestore database and you're ready to go in the next part of this series, I'll show you how to read and write data to your new database. That part of the series is coming out on September 20. So if you're watching this and it's already past that date, then keep watching because I'll link to that part two in the end screen of this video. Now, before we end off this video, quick shout out to my CWC plus program in there, we have a Swift UI Databases course where we build full fledged app using Firestore database and core data. So definitely check that out if you're interested. Lastly, if you enjoyed this video and you want to see more, consider subscribing to the channel and give this video a thumbs up. I appreciate you. Thanks for watching. And I'll see you in the next video.
Info
Channel: CodeWithChris
Views: 4,915
Rating: undefined out of 5
Keywords: firestore tutorial, swift package manager, swiftui firestore, swiftui firebase, get data from firestore swift, firestore swift, firestore swift tutorial, writing data to firestore, firestore ios, firebase ios, firebase firestore ios tutorial, firestore ios sdk, firestore ios swift, firestore ios example, firestore setup, xcode, database, firestore database, firebase, app development, cloud firestore
Id: 47dL5zg9h7Q
Channel Id: undefined
Length: 15min 15sec (915 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.