Intro to Databases (MySQL, CloudKit, Firebase, Core Data, Realm)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey cuckoo what's going on today I'm gonna go over some database options and what I like and what I don't like about each one specifically we're going to go through MySQL cloud kit firebase core data and realm now that's a lot of options to go through so I won't spend that much time on each one I'm gonna give you an overview how data is stored how much it costs and pros and cons for each of the options so alright let's get started undoubtedly MySQL is the most popular out of all the options we've listed traditionally MySQL has been used for web development and powers many websites including my own the most popular content management system WordPress which is installed on over 16 million websites also runs off of MySQL however what's right for web development doesn't mean that it's automatically right for app development before you can understand the difference let's talk about clients versus servers think of a server as a computer sitting somewhere whose sole job is to receive requests and send responses in the web development scenario we call this the web server your database sits on a database server when you sign up for some website hosting and you create your website those files also sit on a web server your browser like Safari and Chrome is called a client when you launch the browser and you visit your website your browser makes a request to the web server the web server then requests the data from the database server combines that data with HTML and CSS and returns it as a response to your browser then your response renders that HTML and CSS code on the screen for you to see so that's what happens in web development in app development your app is a client just like the web browser it cannot connect directly to the database server it needs some sort of middle layer just like the web site files in the web development scenario for app development we call these files web services these web services sit on the web server and wait for requests when your app makes a request to the web service the web service makes a request to the database server and returns that data in some sort of format that your app will understand typically it will be the JSON format that you learned about in the beginner course so here lies the challenge with using MySQL as a database solution for mobile apps you need to build the app client and the web services yourself whereas in web development the browser is already built for you and you only need to construct your website now I'm not saying that we should never use MySQL for app development for example you might already have a MySQL database full of data and you're looking to build an app to serve that data up another example is that you already have a membership site where all of the user accounts are stored in a MySQL database and you're looking to make a companion app for your website or maybe you are already familiar with using MySQL SQL queries and writing web services then go ahead and use it for your app too however if you're starting a brand new app that is free of any constraints then consider all of your options first next up we're going to talk about how data is stored in MySQL data is stored in a MySQL database much like how a spreadsheet would be organized for instance you might have a spreadsheet to keep track of all of the employees in your company you name the spreadsheet employees and inside it you create columns to represent different details of each employee then each row in the spreadsheet would represent one employee in MySQL you have tables instead of spreadsheets so you'll have an employee table in my SQL table is organized exactly like your spreadsheet you'll have columns for the different pieces of data you want to track per employee and each row in your table will represent an employee now let's talk about how much using MySQL will cost in terms of costs most web hosting plans come with a MySQL database and they can start as low as four dollars a month you'll also need a domain name so that your app has a URL to hit for your web services this costs around 10 to 20 dollars a year if you're thinking about going down this route check out the links in the description below however if you haven't explored all of the options in this module yet I would strongly urge you to watch all of them before making up your mind next let's talk about some pros and cons some advantages of using MySQL for app development are familiarity lots of people are familiar with how a MySQL database is structured it's also tried and tested MySQL has been around for a while now some disadvantages are you need to write your own web services this may require you to learn a completely different programming language such as PHP also with a low cost web hosting plan you probably won't be getting the infrastructure to support high load scenarios and you also probably won't have any data redundancy so if your database server goes offline then your app won't be able to access it cloudkit is Apple's back-end as a service solution to store your app and user data in the cloud now if this is your first time hearing the term back-end as a service it's basically all of the back-end infrastructure to enable things like file storage and data storage user management and notifications and more all done for you and available through a single API now it sounds great right it is except that these back-end platforms aren't free however most of these platforms have a free tier that allow you to use all of the infrastructure and services for free until you reach the limits of the free tier the great thing about using one of these back-end platforms is that you get a lot of robust infrastructure for your data storage for example data redundancy your data is backed up and synced across multiple data centers so if one goes down the request is routed to one that is online another freebie is load balancing so requests for data get distributed across multiple data centers to prevent creating a bottleneck at one specific server this prevents the scenario where your app is waiting for a long time for data because of high volume so those are just two examples of why I like using these back-end services such as cloud kit now we're not going to dive too deep into how cloud kit works but I wanted to give you an overview of how the data is stored cloudkit uses the concept of containers databases and records your app has one container which contains three different databases a public database where the data is accessible by anyone a private database where the data is accessed by a specific user a shared database where the data is accessible between a group of users within each database you store the actual data in what is called a record each record contains key value pairs just like a dictionary in Swift however each record is of a certain type and you defined these record types in your cloud kit dashboard for instance you might have an employee record type containing these properties and a manager record type containing other properties then in your database you can create employee and manager records each of these records will contain the key value pairs that you defined in the record types furthermore these records are organized into record zones so on the cloud kit page on developer.apple.com there is a pricing calculator that you can calculate how much it would cost depending on how many active users you have and you can see that it scales up as you get more and more active users and even at 10 million active users your total cost is 0 as long as you stay within these limits here now before we mentioned that your app has different databases there's a public one there's a private one and there's a shared one the private and shared one and where you store data that is specific to a user or shared between certain users that storage is calculated against the user's iCloud account limits and it's not counted toward your app limits however the public data that you store is counted towards these limits here now the next question is what is defined as an active user well actually if you scroll down on the page here in this fine print it says that an active user is defined as having active container usage that means they've been using the database capabilities of your app within the last 16 months and then it also tells you some of the fees that are involved if you have any overage I have to start off with the major drawback of cloudkit and that is that there's no official SDK for Android they obviously have an iOS SDK that you can just drop into your Xcode project to work with cloud kit and they also have a JavaScript SDK that you can have a web map connect to your cloud kid database but there's no official Android SDK which means that if you have an Android app you're gonna have to build a set of web surfaces using the JavaScript SDK and then have your Android app send requests to that set of web services which you built another thing is you can store public data on the cloud kit databases however private data is stored on the user's iCloud account that means if you're using cloud kit and you're gonna be storing private data your users must have an iCloud account now this shouldn't be a problem if you're building an app that runs on iOS because as part of the set up process for the device the users have to sign in with an Apple ID or sign up for a new one and that is basically an iCloud account too however if you're building an Android version of the app and an Android user might not necessarily have an iCloud account or an Apple ID and they'd have to go and sign up for one if your app is going to be Apple only such as mac OS iPad app or an iPhone app then Club kit might be an easy solution for you so some pros of cloud kit include it's easy to use if you're developing for Apple platforms only and there's a generous free tier now some cons users must be signed into iCloud to store private data and it's not easily a cross-platform solution even though you can make it work all right so that's cloud kit firebase is also a back-end as a service but unlike cloud kit it is cross-platform in the sense that it fully supports Android meaning that it has an SDK for it as well so if you're gonna build an Android version or a web version of the same app then it's going to easily be able to use the same firebase database as your iOS app does in addition to that the firebase platform also includes a lot of additional back-end features such as notifications user management file storage lytx crash reporting and more there's actually quite a bit more as you can see from their homepage however let's focus on the database aspect of the platform right now the only tough thing about using firebase for data storage is that you have to wrap your head around the way that the data is stored firebase is a no SQL database if you're used to writing SQL queries with a traditional database like MySQL or Microsoft sequel server then it's going to take a bit of practice to get used to using a no SQL database like firebase in firebase data is stored as a JSON document think about how we structured the question data in the quiz module of the beginner course you can literally import that JSON document to the firebase database here's how it would look in your firebase database to access data you would just need to know the path to the element that you need to add data you can add key value pairs to existing objects or create new nodes there's no concept of tables rows or columns that's why it takes a little time to wrap your head around if you're coming from the world of traditional databases in terms of pricing firebase has a free tier but it's not based on active users like cloud kit is your going to have to see how this fits within your budget and within the plans for your app to be honest with you my own point of view is that it's more important for me to get my app up and running as quickly as possible rather than worrying about incurring some extra costs if I'm hitting the limits of the free tier and I think that's a great problem to have and I fully expect to be generating some sort of revenue from my app anyways the only thing I'd worry about in terms of hitting the limits of this firebase free plan is the file storage limits if you allow your users to upload large files that you might hit that cap pretty quickly so some of the pros are the firebase platform has a lot of back-end features firebase is owned by Google who is arguably pretty good at handling data I would trust their infrastructure now some of the cons no ask you all might take some time to get used to query efficiency also really depends on how your data is structured and coming up with the data schema is a big learning curve now compared to the other database solutions we've talked about so far core data is very different first of all MySQL cloud kit and firebase are all remote database solutions that means that the data is stored on a remote server somewhere where all your users can access it core data is different because it's local on the user's device that means that it's not the solution for connected scenarios like generating user accounts or having user generated content core data is a solution where you need data persistence on your local device for instance your app works with a large offline data set that is stored locally on the device now notice that I've been very careful not to call core data a database and that's because it is not core data is an object graph manager and you can think of an object graph as simply a bunch of objects and the relationships between those objects let's take a closer look remember back in the beginner course in the quiz app we downloaded a JSON file which we parsed to get an array of questioned objects although there aren't any relationships but the question objects this is still an object graph with core data we could preserve all these objects in their current state on disk and then bring them back later it's like putting this graph on deep freeze and then thawing it later to get back all of these objects the cool thing is that you don't have to get back all of them you can query and look for what you need and retrieve a subset of your objects that are stored in core data let's take a look at another example let's say that you have an employee class and a manager class the manager class actually has a property called managed employees that stores an array of employee object references we create a new manager object and three new employee objects then we assign these three employee objects to the managed employees property of the manager object now this manager object has a reference to these three employee objects now this is an object graph with relationships we could use core data to save this manager object and then later to get it back however before you can get to this point there are actually a series of steps that you must first do in Xcode there's a tool called the Xcode data model editor which you use to design your data schema well you have to first create your data model consisting of the manager and employee and how they're related in this Xcode data model editor then from this data model you generate the manager and employee classes so instead of declaring the classes yourself you first model them in the Xcode data model editor and then you generate the classes from that and as a side note under the hood core data uses an SQLite database that's something that is hidden from you and not designed for you to peek into the core data framework is free to use and you can simply add it to your Xcode project and start using it some advantages of using core data are it's a first-party library it's created and endorsed by Apple it's easier to use now compared with before it used to be that it took a lot of code to do routine tasks with core data but Apple has now cut down on the amount of code needed so it's the perfect time to get into it and some cons it's not for connected scenarios so it really depends on what you're doing with your app and secondly it might be overkill if what you need to do in your app is just store some settings or store some offline data that isn't a huge volume if you fire up your browser and visit their website at realm IO you'll see that they actually have two products one is called realm platform and the other is realm database the realm database is what we're after and it's open source and free to use the realm platform is they're paid offering that provides data synchronization capabilities to your app if you need it in this course we're going to be using the free realm database alright let's dive in in the previous lesson about core data I told you about how core data can persist your object graph on the local device well realm does the same thing at the end of the day they are both used to persist data on the local device so that it can be retrieved later however the way they persist data behind the scene is different and also how you implement these two frameworks in your Xcode project is also different in the previous lesson you learn that core data uses SQLite behind the scenes while realm just stores the data for your app in a file locally on your device this file can be opened to look at the data inside in fact they have a free tool called the realm studio that lets you open the data file and browse or edit the data with core data you learn that in order to persist objects these objects must be from a class that was generated from the data model which you designed in the Xcode data model editor with realm you don't do any of that you can persist any class you want simply make it a subclass of object which is a realm class that has all of the code to make the magic happen furthermore you dictate which properties of the objects should also be persisted by adding this in front of the object declaration realm database is pretty simple to understand and to get up and running with the realm database is open source and free to use realm studio is also free some advantages of using realm database are it's a lot easier to jump in and use compared to core data you don't need to understand as much about the framework and how it works underneath the hood you can just get up and start using it and pretty easy to understand another advantage is that it's got the realm studio so you can actually browse the data local to the device and you can edit you can delete you can add data into it where compared to core data you don't have that sort of administrative back-end to use now some cons again it's not for connected scenarios so again it really depends what your app is trying to do and for the second one make of it what you will but core data is a first-party apple framework whereas realm is a third party framework so there you have it my thoughts on MySQL cloud kit firebase core data and realm if you want my opinion on which one I would choose personally then stay tuned for the next video because that's exactly what I'm going to tell you guys now I want to turn over to you which one appeals to you the most and is there another database option that you'd like me to cover let me know by leaving a comment below right now and lastly if you'd like this video please give it a thumbs up and don't forget to subscribe for videos every week all right so we're gonna talk again soon bye
Info
Channel: CodeWithChris
Views: 57,488
Rating: undefined out of 5
Keywords: Database, MySQL, Firebase Database, Realm Database, Realm, Firebase, CloudKit, Core Data, Swift, iOS, Coding, Programming, iOS Development
Id: NxyvPltvvBE
Channel Id: undefined
Length: 20min 42sec (1242 seconds)
Published: Tue Jul 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.