Core Data To Do List App for Beginners (Xcode 12, Swift 5, iOS Basics) - Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back to another swift video in today's video we're going to be learning all about core data and all the operations you would want to do with it so here we've got a to-do list app which we're going to be building so we can query out all of our to-do list items here we can hit this plus button and we can enter a new item so let's do uh do demo go ahead and submit that and you can see that actually puts it into our to-do list and the important thing is if we close this and reopen you'll see that it is persisted we can select one of these items and we can go ahead and delete it and actually deletes it and updates our ui and finally we can go ahead and even edit one of these items so i'm going to come in here and just edit this and this is going to be do something else we're going to go ahead and save it and you can see that actually updates there right in our to-do list in real time so first things first make sure you absolutely destroy that like button for the youtube algorithm helps out with all the videos in the channel might even help you learn core data a little faster wink link and that said hit subscribe while you're at it if you're new welcome to the channel let's get into some core data all right we're going to go ahead and get started by opening up xcode and creating a new project here we're going to stick with the app template under ios let's go ahead and call this project core data example you're going to want to make sure your language is swift and your lifecycle ui kit interface storyboard and the most important thing make sure this box is checked for use core data no need to check this cloud kit box go ahead and continue we'll toss it onto our desktop to get started looks like i got a folder with the same name so i'm just going to go ahead and replace it here and once you've got this created let's expand our xcode window to give ourselves a little more room to work and i'm also going to hit that run button with this simulator that i've got booted i believe it's the 12 pro max and we should see our empty application just like so so cool first things first let's get straight into core data so what you'll notice is that by checking that box of use core data you actually got this file created down here which is actually a xc data model now what is this basically this is where you're going to create the objects visually and their properties that you can save into the core data database so if you imagine if you have a struct in swift ui or swift rather in our case we're not using swift ui here and you want to save it into your application you basically need to tell core data what you want to save and what they're called here are entities actually so we're going to come down here and hit this plus button to create a new entity and it's going to pop up something up here and i'm going to go ahead and call this item a to do list let's make sure we get our casing correct item go ahead and hit enter and to edit it if you're not aware you can just click on it and hit enter it becomes editable and we have this item here now that we can save into core data but we want to add attributes to it and attributes uh are basically the properties on our object that we're going to want to go ahead and save so we're going to go ahead and hit this plus button here and it creates a property here i'm going to go ahead and give this a name it's going to be called name and we want to make sure we give it a type so a name is going to be a string and let's give it one more so i'm going to give one more this is going to be created at and you guessed it this is going to be a date whoops not that we want the one right above it we want a date and the cool thing is this is all we need to do here and we can actually have xcode generate a lot of the code rather all of the code that we need to reference this in our code so on the right panel here you'll see there is this box for code gen and you can see there's three values here there's manual class definition and category and extension so you can actually do it like this and when you run your application it's going to go ahead and actually generate the code on the fly but what we're going to do is a little different we're going to change it to be manual so we can see another way to do it and after we do that we're going to come up to our toolbar and we're going to go to editor and in here you want to find create ns managed object subclass go ahead and hit that and we'll talk about what this is in just a moment all the defaults should be good to just hit enter three times and it's going to create some files for you you'll notice two things here so let's uh firstly drag this into our yellow folder here and i'm gonna be a little organized here and select these right click and do new group from selection and we're gonna go ahead and call this a to list item so let's take a look at what it created so first it created this public class which is marked as a subclass of ns managed object and then it also created this other file here which in which is an extension of that class which has some uh pre-filled out code which is core data related what is this code so the first thing here we have is this fetch request and the fetch request is how we can actually query out all of the to-do list items we've saved in core data and then here we have the actual properties that get saved inside of core data so the name and the date so you'll notice we have this ns managed thing going on here so what is ns managed basically anything that you want to save in core data you need to work with a managed context you can think of it as an intermediary between the actual saved data in the database and your app so instead of talking about theory let's uh go ahead and do an example before we write that code in our view controller to list out our data and enter some data one thing to take note of which is really important actually here in your app delegate is when you check that box to create your project with core data it actually puts some more code in here for you the first thing you'll see is this persistent context rather persistent container and the persistent container is more or less your database this is where your data you can access it where you can read it and save it and speaking of saving it actually also gave you this handy little save context function on here that you can actually call it actually checks if you have any changes on your context and it does a save of your context but we're going to be writing some of our own code out so let's let's jump to our view controller and let's get into the nitty gritty of it so we're going to create a basic to-do list app and in a to-do list app we're going to need to get all of the values that are saved so let's create a function for that let me also bump up the font size a little bit here so it's nice and large so we're going to say get all items we're also going to want to have a function to create an item and it's going to take in a name we're going to want another function to delete an item and this is going to basically take in a simple item which is a to-do list item which if you recall is that generated class that xcode gave us when we created it and let's see we want to get them all we want to create them and let's also take a look at updating a core data item so we're going to go ahead and create a function called update item and once again in here we're going to pass in a to do list item so i'm going to go ahead and we're going to implement the core data stuff first that's the core part of the video and then i'll also hook it up to some some nice ui with a table view but let's do this stuff first since it's basically the point of this video so how do we get all the items out of our database that are to-do list items so we're going to say let items and we're going to basically want to query out of the database all the items and before we do that we actually need to get that context thing that i was talking about before and we need to get it uh in a global way so we can use it in all of our other functions here so bear with me we're going to add something here we're going to call it context and it's going to be equal to ui application shared and we want to get the delegate of the application and we're going to force cast it to our app delegate now let's put this in some parentheses and off of this guy we're going to get the persistent container see if i can find that here here it is persistent container and off of this we want to grab the view context and the context is where you can actually go to perform objects in the core data database so in our example here of getting all the items we're going to say let items and we're going to add here on the other side of the equal context and we want to basically we want to fetch actually a with a request and you'll see that the fetch actually takes a ns fetch request and we can actually just use the first constructor there but what do we pass into here now if you recall in the generated code if you go to the extension here on the to-do list item it actually gave us a class function of fetch request so let's go back to our view controller and actually you can use that directly and what's super nice is that it generates it for you so we can be lazy about it we don't have to write that code ourself so here we can say fetch request just like that and items actually now if you start typing it items will be a array of any we're going to want to go ahead and cast it to be a array of um actual to-do list items so now we're getting an error here that this call can throw so we want to move this whole statement inside of a do catch block so here we're going to say do we're going to put that there we want to prefix this with try and we want to put a catch down here and i'm not going to do anything here but in a real app of course you want to go ahead and handle your actual uh error it's just going ahead and yelling at me that we're creating items here but not using it but we can leave that put for now so that's how you go ahead and get all your items out of the database and core data how do we create an item so creating an item is also very very simple so just like you would create any other instance of a class we're going to say a new item is a to-do list item and we're going to create it with a context and similar to what we have done up above we'll pass in the context here which is this guy we created right here and this is how we're going to create a to-do list item with that context now on here we can go ahead and assign its properties so the name is going to be the name the created at will be the date now you're not done after you've created it we still need to save it into the database so how do we go ahead and save it in the context well similar to before in a do catch statement we're going to go ahead and we're going to say try context and we're going to call save on this guy make sure you put a try before it and toss it into a do catch otherwise you'll get the same error that we were hitting up here now let's go ahead and do the delete function here so pretty simple we're going to say context we're going to call delete and then similarly we want to call save on the context so we're going to be lazy and copy and paste this code super simple that's how you delete something from core data and uh of course we're going to take a look at updating as well so updating here we're passing in an item presumably we want to go ahead and update the name on it so we're going to say name parameter let me go ahead and actually call it new name and here i'm going to go ahead and say item dot name is going to be new name and once again once you've assigned either reassign the property you want to call save on the context so i'm just going to once again copy this and paste it and that's how you can get all the data out of core data how you can insert new data that you can delete and how you can update now let's go ahead and just hit that run button and make sure it's working and we're going to hook it up to some pretty nifty looking ui for our to-do list app so cool definitely compiling definitely working let's go ahead and put together that awesome table view for our to-do list so i'm going to put a comment here for core data just so we can organize our code a little bit here we're going to have a title and this title is going to be let's call it to-do list or we can call it a core data core data to do list just like that and we're going to want a table view if you're not familiar with table views i've got a bunch of videos on table views already on the channel so definitely go take a peek there to learn more about table views i'm going to go through this fairly quickly in terms of actually setting it up for the sake of time and because this isn't really a video about table views but here we're just creating a table view going to register a cell with it we're going to want to make sure we add this as a sub view to our view and assign this guy's delegate and data source and of course we need to give this guy a frame as well now we're gonna see some errors here because we have not conformed to the delegate and data source protocols here so here we're going to say the delegate and data source protocols and we want to bring in those required functions so number of rows go ahead and toss one there for now we're going to hook it up to the number of to-do list items we have and here we're going to say sell for row at index path let's see if i can find it i'll just stop being lazy and type it out and here we're going to want to go ahead and create our cells i'm going to go ahead and say table view dq a reusable cell with an id of cell for identifier and we're going to return that cell just like that here we're going to say the text label text is going to be somewhat text we'll just say hello world for now all rights now we want to have a property that's global that can drive the number of cells in our table view so here we're going to create a private var and it's going to be called model and this is going to be an array of to-do list items and what we can actually go ahead and do in here now when we say get all of these items we can actually assign that to our models since this is going to actually go ahead and return an array of to-do list items and after we do that on the table view we can actually call reload here and that's just going to go ahead and reload our data in the ui but because it's a ui operation we want to make sure we do it here on the main thread if you're not familiar with this basically anything ui related should be done on the main thread anyways we're going to go back to this and for number of rows we're going to return the number of models that we have so the number of to-do list items so looking pretty good now in here we're going to say the current model or the current to-do list item in our models is the nth position and from it we can assign this as model dot name just like that beautiful and uh if we actually go ahead and run it we should be basically be able to compile and launch our app but we're not going to see anything in it we definitely need a way to add a to-do list item and we're going to add a plus button up here at the top right to do that now we're going to jump into our main storyboard for just a moment here and i know i'm going a little fast and i'm going to review everything before we wrap up the video here but select your controller in the toolbar go to editor and you want to find embed in and we're going to toss this controller in a navigation controller that'll give us a nice looking navigation bar up here and i personally prefer the large title so open up that right panel which is the attribute inspector under this tab just go ahead and check that and give your app a run and you should now see a title up here which we gave in our actual code and now let's go back and add that plus button so we can take a look at how we can bring in new to-do list items so how do we bring in that plus button so it's going to be a navigation item so here at the bottom of muted load i'm going to say navigation item and we're going to assign the right bar button item as a ui bar button item we're going to give this guy a title we could instead of giving it a title give it a system bar by an item and i believe it's called add target itself and action is going to be did tap add which is a selector aka a function that we have not created yet so let's go ahead and create it right below it now what do we want to do when we tap this add button well uh just for the sake of keeping the simple we're going to show an alert and that alert's going to have a text field or we can type in a to-do list entry so let's go ahead and create an alert which is going to be a ui alert controller with a title of new item enter new item for the message the style will be an alert once we create the alert at the end we're going to want to of course present said alert we're going to want a action in the alerts we're going to say add action we're going to say ui alert action and this is going to have a title of submit a style of cancel and a handler in here like so we also want to add a text field on here by just calling add text field like this and let's see we've got a configuration handler that we can be lazy and pass in nil for now once we go ahead and submit on this alert we want to get the text out of the actual alert uh the text field of the alert rather so here we're going to say guard let field equals alert dot text fields dot first and let text equals text field what did i call a field rather dot text and once we get both of those out of there let's also guard that the text is not empty because if the text is empty we don't want to actually go ahead and enter anything into core data because that's a little silly if it is not empty we're going to go ahead and we're going to call our function that we created before which is a create item and the name is going to be our text now we're going to want to go ahead and make this weak self so we don't create a memory week so go ahead and throw a question mark right there to make it optional if you're not familiar with memory leaks and retain cycles i've got a video on that as well but that's how we're going to take care of creating that so before we move forward let's go ahead and make sure that actually works so let's take a look at our create function down here after we create something we actually want to do something that we're not doing here after we create it successfully we actually want to say get all items and what this is going to do is now it's going to refresh our models and reload our table view here so let's go ahead and hit command r to build and run hopefully we have no errors looks like we don't we're going to hit this we should see our alert here and let me also pop up that keyboard by going to i o keyboard and toggle that software keyboard so we can see what it'll actually look like on a device so here i'm going to say uh let's say get milk why not it's not actually something i need to do but sounds like something people would have to do so there's our get milk let's go ahead and create something else to create youtube video all right now the coolest part is let's go ahead and delete this app rather instead of deleting and close the app and delete it from multitasking and reopen it and the coolest thing about this is uh is that that data should actually be showing up here so let's see what we missed because it's not so what we actually missed is we're not actually calling that get all function here so in view to load let's go ahead and call that get all items before we set up the table and your data should persist uh hence a database and there is our data so we can close the app here open it up and we still see our data now if you recall we're also saving the dates in here so let's uh let's actually show that here in the cell as well so where is our cell for row function all right here it is here what we can go ahead and do inside of a string we can interpolate the actual name as well as the created at which is a date just for the sake of making sure that this works once we go ahead and run it you can see we are actually saving the milk get milk and the date here now they're wrapped in optionals so you could unwrap them but for the sake of uh making sure our app looks a little nicer let's do some undo's there to just have the name once again just like that now let's take a look at how you can modify or delete these entries so right now when you select one of those items nothing happens one of those table view rows i should say so we're going to implement did select row at index path we're going to go ahead and call deselect on it at the index path with an animation now in here we're going to want to show an action sheet with two options to either delete or edit i'm going to be lazy and copy and paste this alert we're going to modify some code this is going to turn into a sheet this is going to be uh let's call this edit nil for the message this will be an action sheet we don't want to add anything on here this is going to be a sheet and let's see what do we want in here so the first option that we're going to add into our action sheet is going to be cancel with no handler so that's perfect just get rid of all that code down here we're going to want to call present on the sheet itself so let's see looks like we've got an error here for actually adding this let's just go ahead and retype out this code for the sake of making sure we get it done correctly so add action ui alert action with a title of let's do cancel cancel and nil and we're going to want two more this one is going to be edit delete defaults and destructive will give you that red title which looks a little scary all right we're going to do that and do that so when the user hits this delete option in here it's pretty simple what do we want to do we want to say self and we want to go ahead and say delete item and pass in the item now we need to get the item out first that we selected so up here we're going to say item is models and in models we want to get the selected item which is at index path dot row just like that you want to make sure we go ahead and do week self here as well to make sure we don't create a retain cycle now edit is going to be a little different for edit we actually want to copy the page the code up here again and paste it which is all this alert business so copy that throw that in there and let's take a look at what we want to do so here we're going to change this to be edit item we're going to say edit your item the name by default we are actually going to set in this field so here i'm going to say alert text fields the first text field and believe we can just force unwrap that hopefully we should be able to all right first and we're going to assign its text to be the item dot name this way your text will be pre-filled in when you tap on that edit and let's see what else we want to do here so here we are doing submit let's change this to save and instead of creating a new item here we're going to go ahead and say update item pass in the item and we're going to go ahead and instead of calling this text we're going to call this a new name new name and make sure you change it here as well and we're inside of a closure here itself so go ahead and do self and i know we didn't put weak self up here but just for the sake of not having to add a bunch of more optionals we should be in business so let's go ahead and hit command r to build and run let me change this to dark mode so it looks a little cooler and now let's add some more entries the next one i'm going to add is go on a run and we get it added there let's get rid of this milk business so we're going to select it it's telling me i can edit it okay so let's say i remembered that i needed something else we're going to hit edit and i'm gonna change this whoops it's like i hit the option there to get dictation but we're gonna go ahead and change this let's try that one more time so i don't keep seeing that pop up there so we're gonna hit get milk we're gonna hit edit and we're gonna go ahead and actually delete all of this and i'm gonna change this to get candy because that's way nicer than milk we're gonna go ahead and save it and you'll notice here that we didn't actually update this so what gives so let's take a look at our update function so where is our update function uh what we forgot to do here is once we go ahead and change it we want to go ahead and call that uh get all items function again so we get the refreshed items and i believe we're going to need to do that here as well and let's go ahead and build and run so what happened there is our database should have gotten updated but our actual view didn't update so let's try that one more time so i'm gonna go ahead and edit this and this is now going to be go lift weights we're going to go save it and you'll see that it changes in real time just like that looking awesome let's go ahead and select that delete it and you can see it deletes as well and of course the most important part of all of this is because it's core data and it's a database and it persists on the device if you close and reopen the app all your data's still there you can go ahead and edit it and it'll update you'll notice this one remained updated and yeah that's basically a fairly good primer i would hope of core data so let's do a quick review and then we'll call it a wrap for this video so before we review if you haven't done so already make sure you destroy the like button helps out the video and the content quite a bit but let's get into that review so the first thing we did is we came into this xc data model and we created a new entity with the plus button down here we called it a to-do list item we went ahead and added two properties and here of type date and string in the attributes inspector if you open this up and select it here you can choose how you want to do code generation we made it manual for the sake of learning a little bit more once we made it manual we went to editor in editor we clicked on create ns managed object subclass anything that you want core data to manage needs to be a subclass of ns managed object now the reason they created this and it's empty is because if you wanted some custom functionality you can put it in here however it also created the basic stubbed out code that you need in this extension here which gives you the fetch request as well as your managed properties the next thing we looked at in the app delegate was the i can find the app delegate there it is the persistent container which is where all your actual data gets persisted hence the name we took a look at this convenient little save context function right here and then the rest of our time was spent in the view controller which in all reality is actually not a lot of code even though what does it look like looks like about 148 but it's not that much actually so if we disregard actually ninety percent of this the important stuff for core data is we created a context now we got our app delegate we cast it if we made it optional you would have to make your actual property optional so this is a persistent container you would need to change this to be optional and your actual object here now would need to also be optional so you would just need to change all of your calls down here which is why for this example i just forced unwrapped it in an actual application you probably don't want to do that just for the sake of being crash free here we have a table view we configure the table view and add a plus button at the top right let's take a look at our actual database functionality down here so here to actually get all the items we simply create a fetch request of the to-do list item fetch request on the context and that basically just queries out all the to-do list items refresh the table view in the actual function to create an item we create this item with a context which is our global property we just looked at we assign the actual values here for created ad we're just assigning the current date and the name is the parameter that comes in and most importantly don't forget to call save on your contacts otherwise it won't actually save in the delete function we just call it delete on the context for the item you want to get rid of make sure you save and finally for update we go ahead and pass in the new name for the item we want to update we assign it and at the end of it call save and refresh our data and there you have it that is how to use core data in your apps in swift most apps these days have some sort of database and persistence so let me know what you guys think down below in the comments i always love hearing from you guys video suggestions did you find it helpful questions clarifications if you just want to say hi down below i find it amazing that so many of you guys get any type of value from these videos it you know really inspires me to make more videos so definitely say hi down below hit subscribe if you haven't done so already i think like 70 of you watch consistently but have not hit that big red button so make sure you hit subscribe and join the party for ios videos swift swift ui the whole nine yards thanks for watching i'll catch you on the next video
Info
Channel: iOS Academy
Views: 23,949
Rating: undefined out of 5
Keywords: swift 5 tutorial, swift 2020, swift, swift 5, swift tutorial, swift for beginners, swiftUI 2020, swift programming, swift basics, swift apps, make iPhone app, swift make first app, core data, swift core data, core data basics, core data swift 5, core data swiftui, core data swift, swiftUI core data, core data to do list, wift to do list, to do list swiftui, to do list core data, swift core data 2020, swift database, swift save data locally, core data 2020, coredata app
Id: rjHBINtpKA8
Channel Id: undefined
Length: 32min 26sec (1946 seconds)
Published: Fri Dec 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.