Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  • Overview
  • Transcript

5.3 Basic Core Data Interaction

All right, let's start to see some code with interacting with core data. So the first thing that you need to do is we're gonna start off in our FeedsTableViewController, cuz this is where ultimately most of the interaction with core data is going to be. Now regardless of where that is, you're going to have to request the functionality of core data. In order to do that we are gonna have to add an import statement. So we're gonna come up to the top of our file and we are going to import core, core data. Now simply by doing that, we are gonna get access to all of the nice little features of core data within our class here. So I'm gonna come down a little bit and I'm actually going to save you a little bit of pain of watching me type all this out and I'm gonna paste in a few bits of code here and I'm gonna walk you through them. That's gonna show you the basic interaction between being able to insert some data into core data and getting it out. So, I'm gonna come in here and I'm gonna paste a couple helper methods here. All right, so I've pasted in three methods here, or three functions, SaveFeed, FeedExists, and GetFeeds. Now if you take a look at this, they're fairly straight forward, and actually, some of the functionality is actually very similar, but just so you can see the pattern of how this is going to work. Now, SaveFeed is what we're going to use to actually persist one of our feeds and its associated articles. So we're gonna pass in our feedModel. We're first gonna check to see if the feed already exists there, because there's really no sense in adding in duplicates if we don't have to. Then these next two steps are gonna be very redundant, over and over again, whenever you're dealing with core data. Now if you recall, in a previous lesson, I pointed you into the AppDelegate and you saw all this extra code down at the bottom about the Core Data stack, dealing with the directory, the managedObjectModel, the persistentStoreCoordinator, and the managedObjectContext. Now, you can definitely pick through here and see how these things work. I'm not really too concerned about it for this lesson, since we're just looking to get the basics down. But, one of the most important parts that you're gonna be interacting with throughout this application is the managedObjectContext. So if we come back into our FeedsTableViewController, the first two steps that we always have to do is we have to get access to our appDelegates, since that's where all of this stuff has been created for us when we checked that little use core data box when we were creating our project. So to do that, we're using a special UI application class, which has a sharedApplication method on it, it has a delegate property. And we can cast that as our appDelegate. Now we're casting that as our appDelegate because that's ultimately the name of our class. Then we're going to use that appDelegate to get our managed object context, which is merely a property that was actually created for us so we can use it. Now later on in our application, all we have to do is use this managedObjectContext that's ultimately going to get data in and out of core data. So now the basic process here, when we want to save something into our core data managedObjectContext, is we're gonna always create an entity. And the way that you do that is by using the NSEntityDescription class and we want to create an entity for name. This is where we're gonna specify what the feed or what the entity name is. And we want to specify in which managedObjectContext. So that ultimately means you can have multiple contexts within your application should you so choose. So that's gonna create an entity for us. And then ultimately we want this to be a feed, that's why I created those entity classes, or sub classes, in the last lesson. So I'm gonna create a new, managed NSManagedObject for the entity that we just created within core data. And we want to make sure that it is inserted into our managed context. And we want to cast this as a feed. So everywhere else within my application, or at least within this method, I can refer to this as feed. And it has all the properties you would assume of, of feed to have from that class, a title, a URL, and ultimately articles. So what we're gonna do is we're gonna set the properties of this entity to the properties that match the model. Then we're going to create a mutable set here of articles which is just a set and in a set if you're familiar with that within iOS to hold a collection of information and mutable just means I can add data to it. So then for each of the article models so this is just a simple for in loop. For each of the article models I'm gonna loop through them, and for each one of them, I'm going to create a corresponding entity as an article entity, just like I did up above. I'm gonna do the same thing here for creating an entity, and then cast an entity in to an article, set it's appropriate properties, add it to my collection of articles here. And once I loop through all of them, I'm going to make sure that I assign those articles to my feed entity, and then I'm going to call save on my managedContext. Now, this is just a little process here, you can create an error variable, and then call save and pass that in. And if something bad happens, then we get that information in the error. And then I go ahead and just print that out to the console at the bottom of the screen. Hopefully we won't need to use that, but, just in case, we can do some troubleshooting. So that is the save feed. Now, FeedExists is gonna do the same thing. It's going to get our appDelegate, it's going to get our managedContext, and then it's going to do a retrieval. Now, retrievals are a little bit different. You're gonna deal with something called a FetchRequest. Now, a FetchRequest is just another class, and in this case, we're going to create a FetchRequest based on an entity with the name of Feed, so I wanna looked for feeds within core data via this FetchRequest. Now you can leave it just like that, and if you were to execute a FetchRequest with only this, it's going to basically do a select all. It's gonna give you all the feeds that it finds within our persisted store. But I don't want all of them. I wanna make sure that the one that I'm trying to save doesn't already exist. And basically the uniqueness of this is going to be based on the URL. So I'm gonna look for, I'm gonna create what's called an NSPredicate, which you can kind of equate to, maybe, a where clause if you really wanted to. It's kinda working in a similar way. So it's going to look for any feed in there which has a URL property that's equal to the URL that's being passed in as a parameter to our function. And then we're going to set that predicate on the FetchRequest and we're just going to execute this FetchRequest and we're gonna have that same kind of whole NSError situation going on. And then we simply look to see if there's more than zero results on my results. And if there is then that means it already exists and if there's not then we're gonna return false which means it doesn't exist. And then finally we have GetFeeds which is gonna do something very, very similar to what we have here in exist except it's going to return an array of feed models. So how's it gonna return FeedModels if it's having the FeedEntities stored within core data? Well I'm gonna have to do a little massaging of the data. This is where you might think about things like factory patterns, and things like that if you really want to. I'm just kind of keeping it relatively simple. But you could get as elaborate as you'd like. So once again, I need my appDelegate and my managedContext. I'm gonna create my FetchRequest that's looking for the entity name of Feed. And then I'm going to execute that FetchRequest. And then once I get those back, I'm going to loop through all of my results. And I'm going to make sure that I'm casting those as an array of feeds cuz I want to deal with those objects that I've created, those subclasses. I don't wanna deal with the NSManagedObject and have to do some helper methods to go through and set and get all these properties. So I'm gonna, for each one of those results I'm going to create a new model. I'm going to set the appropriate properties. I'm gonna loop through all the articles as well. Create articles for each one of those, and I'm going to assign those article models to my feed model, and then ultimately, once we get down to the bottom, I am going to return all those models. So those are gonna be the three basic helper methods that we're gonna use throughout the rest of our application. We're gonna have to add in some tweaks here and there as we start getting into things like updating and deleting, but for the most part that's gonna be enough for us to get started. So, now that we have these helper methods in there, let's go ahead and put them to use by interacting from the UI down to core data.

Back to the top