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.

download-excodeproj

I used the Single View Application template to create the tutorial’s iOS project with these options.

Figure A

On the final project creation screen, I selected the “Interactive Swift Programming” folder and ticked the “Source Control” checkbox, before clicking the “Create” button.

scdfs-fig1-1

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.

scdfs-fig1-2

Next, I selected the data model file and perform steps shown in imaged below to create a class for the Book entity.

scdfs-fig1-3

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:

book-entity-class-code

I selected the data model file and include the application name as part of the entity class name.

scdfs-fig1-4

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.

scdfs-booksview
Figure 1: Books View
scdfs-addbookview
Figure 2: Add Book View
Programming Tasks

Here is a list of Core Data and standard programming tasks you’ll learn how to do in this tutorial series:

  1. Insert a managed object in a database file
  2. Fetch all managed objects from the database file
  3. Display fetched managed objects in a table view cells
  4. Update a managed object in the database file
  5. Delete a managed object from the database file
  6. Delete a managed object from the table view
  7. Search for a managed object in the database file
  8. Display search results in the table view cells
  9. Cancel the search task
  10. Validate required input fields
  11. Query the database for duplicate entry

That’s all for today. Next week, I will bring you workshop 1. Until then, comments are welcomed! 🙂

Tags:

3 Responses

Leave a Reply

UIDocument Demystified: A Step-by-step Guide on Local and iCloud Document Storage

 

Archives