The course is part of this learning path
This course focuses on a concept known as core data, which will allow us to store our data in a database. We'll then show you how to apply this concept to a real-life app which we will build called Art Book.
Learn how to create a local database allowing users to save data on their phones using core data
This course is intended for beginners who want to learn how to build apps using Swift.
To the most out of this course, you should have some basic understanding of programming and computer science in general.
Hi, within this lecture, we're going to add delete functionality in our app. Right now we can see the data, we can see the details of the data and everything, but we don't have delete functionality. And we have seen how to delete values from tableViews before, right? We have to use commit editing style function, but we don't know how to delete those values from the Core Data and we're just going to learn in this lecture. So, go to the bottom of this View Controller and I'm not in the View Controller I believe. I'm in the detailsVC. I have to go to View Controller because that's where my tableView is. Now, at the bottom of the View Controller I'm just going to call for editing, editing style, commit editing style, that is the one that I'm looking for. Remember we can have this delete functionality in here. We have to just check for the editing style, and if it's delete, we're just going to do what all we have to do. So, I will say if editing style is actually .delete then I'm going to do my thing in here. And the deleting functionality in the Core Data and it has to be double equal sign of course. And deleting functionality in the Core Data works like this. First, we get the element from the Core Data, okay? So, we do the patch again and then we're just going to delete it and save it one more time. So, I'm just going to create our app delegate, I'm just going to go over and create a fetchRequest one more time. So, this is going to be exactly what we're doing in the detailsVC, so you can just copy and paste as well. But, I believe we should write this, so it will be a practice for us. So, I'm going to drive this context from the appDelegate.persistentContainer.viewContext. And then later on I'm just going to create my fetchRequest and remember this is NSFetchRequest, and remember the reason that I'm doing this before I delete the data I have to get it from the Core Data and then delete it. And in the end actually, I'm going to save it again. So, in here, I'm doing the same thing as I'm doing in the viewDidLoad but I'm just going to write it from the scratch. So, NSFetchRequest, if you open parenthesis it will ask you for an entity name, okay? So, entity name will be paintings and we have to just say NSFetchRequestResults one more time. So, I think it's getting boring in here but at the same time you're doing this practice so you won't forget how to create appDelegates, how to create fetchRequests. So, let's say NSFetchRequest.predicate will be NSPredicate. Why I'm doing this filtering thing? Because we're just going to get the selected item, right? The item that our user is trying to delete. So, we're just going to get it using the id of the selected item. So, you know how to do that. We're just going to use our selected ID from the idArray and make it equal to id%@. So, just say id=%@. So, with spaces over here, and the argument is, of course, going to be the uuidstring. So, this is going to be idstring, idArray [indexPath.row], okay? So, this will give me the selected items id and I'm going to convert this to be a uuidstring. So, this is exactly the same thing that we have done in the details View Controller. So, we're going to get this, we're going to get those values from the Core Data and before we do that, let me just not forget this returnObjectsAsFault thing. And then I'm going to use the context and I'm going to fetch this as try context.fetch. And the fetchRequest is fetchRequest. Of course, we're going to have to do this in a do try catch structure, okay? do try catch, so print error if any error occurs. And then later on I'm just going to check if results.count is actually larger than zero. So, there isn't any reason not to be but just for being on the safe side, I'm just going to check it anyway and I'm just going to do the same for loop that we have been doing and cast this as NSManagedObject. And this will only give me one result. However, we got an array in here. So, we have to go into for loop anyway. So, I'm going to find the id, okay? I'm going to get the id from results.value and it doesn't pop up because I misspelled it the result.value and for the key id as UUID, okay? And even though it doesn't make sense to you right now, I'm just going to check if this id is equal to the id that is coming from my idArray. So, I'm just going to say if id == idArray[indexPath.row], okay? So, what does it mean, why I'm doing this? I got the selected id from the idArray and I use that id to get those information so it has to be equal, right? So, I'm just comparing the same ids, but I'm just doing this to be on the safe side because right now I'm just going to delete the value from the Core Data by saying context.delete and there is no return from this, okay? I'm just going to delete the object. I'm just going to say delete result and there is no way back, so I'm just doing this just to be sure. and after I'm deleting this result and result being the NSManagedObject I'm just going to remove everything from my arrays as well. Okay, I'm just going to say nameArray.remove, IdArray.remove in order to synchronize my datas, okay? And for the last thing I'm just going to have to say tableView.reloadData because I've just changed the data source. And that's it. Right now, I deleted the NSManagedObject meaning I've deleted the record; now I have to save the current context. So, I have to come over here and say context.save. And again it will give me an error saying that you have to do this inside of a do try catch, so I'm just going to create another do try catch inside of my current do try catch, okay? But in here just say do try context.save catch. And if it's any error, you can just print the error. So, that's it. That's how you delete the value from Core Data. You get the data, you check if it's the data that you want to delete and you delete the data or you delete the NSManagedObject and then you save it. I'm just going to say break in here. Why do I use this? Because if I delete the data, I'm checking if the id is equal and everything, if I delete the data, if this id is actually the id that I'm looking for then I want to break out of for loop. It will give me one result really because we're working with id not names not any made up values that we have come up with, we are working with id. So, it's going to be only one loop in this for loop. But, to be on the safe side, I'm just going to use break in here. So, if you decide to use something rather than id in your own codes, you have to use this because once you delete one item, then this loop shouldn't be continued. You have to break out of this loop. So, if you say break, it means that end this loop, just go out of it and do what you have to do next. And again in my case, it isn't necessary to use this. But, it in some cases, if you're doing this with a name rather than id it should be necessary. So, let's see, let's run this and let's see if we can actually delete the values from Table View. Let's say swipe left and here you go; we can delete them. So, let me delete this one and let me delete this one as well, yep, they're gone. So, let's see if we managed to delete them from the Core Data. If we run it from the scratch one more time, as you can see, we don't see the related values, so it works. So far so good, let's stop here. And within the next lecture, we're going to finish our app by doing the rest of the improvements.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.