In this tutorial series you will learn how to use the Swift Programming Language, the NSFetchResultsController class, and the Core Data frame work to implement various Core Data tasks in an iOS application. Before we get started, I assume the following:
- You are not a newbie to the Swift Programming Language
- You are note a newbie to Xcode IDE
- You know how to use the table view object
- You’ve read the Using Core Data tutorial; it was written in the Objective-C Language
The NSFetchedResultsController Class
The fetched results controller (
NSFetchedResultsController) is a utility class and it bundles together all the Core Data fetch related functions you need to perform when providing data for a table view or a collection view. The fetched results controller provides a way to set up a fetch request so that the results are returned in sections and rows, accessible by index paths. In addition, the fetched results controller can listen to changes in Core Data and update the table view or collection view cells accordingly using delegate functions.
You can do all of the above without the use of the fetched results controller. Further more, if you aren’t using Core Data as your app’s data-model, then don’t use the fetched results controller. In summary, the fetched results controller ease presentation of managed objects in your application’s table view or collection view.
Overview of The Xcode Project
You will need an iOS project to implement programming tasks. Well, I’ve already created one for this tutorial. All you have to do is click this link to download it.
I used the Single View Application template to create the tutorial’s iOS project with these options.
On the final project creation screen, I selected the “Interactive Swift Programming” folder and ticked the “Source Control” checkbox, before clicking the “Create” button.
By selecting the “Use Core Data” checkbox in the project options window; Xcode created the usual files and folders and an empty data model file called SwiftCoreDataFromScratch.xcdatamodeled in the project. Xcode also setup the Core Data stack in the AppDelegate.swift file. I configure the data model file by adding a Book entity with 3 String Attributes.
Next, I selected the data model file and perform steps shown in imaged below to create a class for the Book entity.
Here’s the source code of the Book.swift class file. You’ll use the class to create and insert managed objects in the project’s SQLite database file:
I selected the data model file and include the application name as part of the entity class name.
This prevent the compiler from displaying this warning in Xcode’s Debugger console, when you run the application in the iOS Simulator:
CoreData: warning: Unable to load class named 'Movie' for entity 'Movie'. Class not found, using default NSManagedObject instead.
When you open the project in Xcode’s IDE, you’ll notice that I’ve setup the project’s views and view controller class files. Images below shows what they look like in Xcode’s IDE.
|Figure 1: Books View
|Figure 2: Add Book View
Here is a list of Core Data and standard programming tasks you’ll learn how to do in this tutorial series:
- Insert a managed object in a database file
- Fetch all managed objects from the database file
- Display fetched managed objects in a table view cells
- Update a managed object in the database file
- Delete a managed object from the database file
- Delete a managed object from the table view
- Search for a managed object in the database file
- Display search results in the table view cells
- Cancel the search task
- Validate required input fields
- Query the database for duplicate entry
That’s all for today. Next week, I will bring you workshop 1. Until then, comments are welcomed! 🙂