2.7 Filter Data With Predicates
In this lesson, I will teach you about predicates, which are a very versatile way of filtering data. Then, I'll set you up to explore the possibilities of predicates with a custom playground.
1.Introduction2 lessons, 04:06
2.Core Data9 lessons, 1:04:07
3.Conclusion1 lesson, 00:53
2.7 Filter Data With Predicates
Hi, and welcome back to get started as of Core Data. In this lesson we will take a break from our class project and explore the possibilities of predicates in our playground. I have configured this playground to spin up a basic Core Data stack and fill it with sample data. Five states with 50 cities each, having random names and number of inhabitants. Let's start with a simple example, we want to know the cities that have more than 300,000 inhabitants. You can see that a predicate looks very similar to a string formatting. I have added some convenience functions we can use to fetch the data. If I showed a count, you can see that 181 cities have more than 300,000 inhabitants. For reference, let's also output the number of cities at the top. Since the data gets randomized every time the playground is run which is every time we change something. We now have 174 cities matching the predicate. To make it more dynamic, we can change the key to %K which is a key type and set it as an argument. We can also change to inherit a number through an argument using %B. Of course this is the most basic case of using predicate. If you know a little bit of SQL, you will see that some of the key words that are used here are quite similar. I can specify that I want all cities that have inhabitants between 250,000 and 500,000. Now we get as much smaller range. You can also combine conditions. This can be done in line by using the end keywords. As an example lets get all cities that have between a quarter and a half a million inhabitants and the name starts with the letter A. I'll output the name right here. Since the beta changes every time, we don't always get a match. So let's make this a bit safer here and use a conditional assignment fill out with the name and number of inhabitants. Here we get Austin or Albany for example. You don't need to combine your conditions in a single predicate, you can also separate them. Let's extract the city name into its own predicate. To combine them, you can use a compound predicate, that has an array of sub predicates that must all match You can also use relationships as predicate conditions. Let's say I want to get all cities of a certain state. I can change this to use the state that abbreviation and query for NY here. I have added a convenience function that chose the name and state of a city. Here and once again returns Albany, New York. A very handy feature with which you can create a rather complex condition is sub querying. In our example I want to fetch all states that only have cities with inhabitants smaller than a given number. The sub query can also set variables. Here i'm using the city's relationship on the state and set it to $X. And then at the condition that $X, that inhabitants is bigger than a given number. My main conditioners that the object count of this sub query must be zero. Let's try it with 500,000 and up with the count of states that we have fetched. Okay 500,000 might have been too low. Let's crank it all the way up to 975,000, because the maximum is 1 million. Finally we get the state that matches. That's also output it's name like before In this case it's New York. The final feature of predicates I want to show is that we also can use NS managed objects directly in their condition. I could query for all cities that are in a given state. With %@, I can use a state here and I will take the first one off the list of all states as an argument. This should give me 50 cities and it does. As you can see, predicates are very powerful if you know how to use them. I only gave you an introduction but especially string carries can do a lot, although they might be quite expenses to use. In a next lesson we will return to our course project and talk a little bit about performance considerations. See you there.