Now that you know what Core Data is, how it works, and how you can integrate it in your application, the time is right to figure out how to store and query data with it. Adding the Core Data stack wasn't very complex. However, handling actual data in your apps is a bit more complex since you might have to deal with things such as multithreading, or objects that suddenly get updated when you've just read them from the database. Dealing with databases isn't easy, especially if you're trying to ensure data integrity. Luckily for us, Core Data helps tremendously with that.
In this chapter, we will insert some pieces of data into our database and read them. You'll learn how to update the user interface whenever the underlying data changes. You will also learn how you can ensure that you don't accidentally try to read or write objects on the wrong thread and why it matters. To read and filter data, you will use predicates. Finally, you will learn about some of the ways iOS 10 helps in making sure that you always use the correct objects from your database. To wrap everything up, we'll briefly look at what it's like to have an app with multiple NSManagedObjectContext instances and how it affects the way you should set up your code.
In this chapter, we will cover the following topics:
- Storing data with Core Data
- Reading data with a simple fetch request
- Filtering data with predicates
- Reacting to database changes
- Understanding the use of multiple NSManagedObjectContexts