Using Core Data: Workshop 3

Today you’ll be given a bird’s eye view of the CoreDataWorkshop application you will develop in this workshop series. You will then set up the first scene and its class.

The CoreDataWorkshop Application

The Core Data application we will create in this workshop series will enable the user to perform CRUD operations using a navigation controller and three storyboard scenes. Here is a snap shot of what the Project Navigator panel and the storyboard’s scenes will eventually look like in Interface Builder.

coredata-projnav-storyboard
Figure 1

This table shows the storyboard scene that’ll allow the app user to perform a CRUD operation.

CRUD Operation Storyboard Scene
Create a book object in the database file Add Book
Retrieve and display book objects Book List
Update a book object in the database file Book Detail
Delete a book object from the database file Book List

The App’s data will be persisted in a SQLite database file which will contain a single table, as shown in the image below.

coredata-database-schema

Setup The Book List Scene

Now that you’ve been introduced to the application you’ll create, you’re ready to develop the application. Start by deleting the ViewController class from the Project Navigator panel. Next, delete the scene the template created on the storyboard canvas. All that’s left in the CoreDataWorkshop folder (the one with the yellow folder icon), is the Main.storyboard file and the AppDelegate class files. Now, you’ll have to set up the Book List scene.

  • Drag a Table View Controller from the Object Library and drop it on the storyboard canvas.
  • Embed the scene in a Navigation Controller by selecting Editor → Embed in → Navigation Controller.
  • In the Attributes Inspector panel, set the Navigation Bar’s Title attribute to Book List.
  • Set the Table View Cell’s Style attribute to Basic and its Identifier property to Cell.
  • Connect the scene’s dataSource and delegate properties in Interface Builder’s Connections Inspector panel.

Here’s what the Navigation Controller and the Book List scene should look like in Interface Builder.

coredata-scene1

Setup The Book List Scene’s Class

Now that you’ve setup the Book List scene, you need to set up the scene’s class.

  • Use Xcode’s template to add an Objective-C class in the project.
  • Make it a subclass of the UITableViewController class and name it BookViewController
  • Connect the class to the Book List scene by setting the scene’s Class attribute to BookViewController, in the Identity Inspector panel.

Declare these property variables in the BookViewController.h file:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface BookViewController : UITableViewController

// A pointer for accessing the AppDelegate class properties and methods
@property (retain, nonatomic) NSMutableArray *tableData;

// A pointer for accessing the AppDelegate class properties and methods
@property (strong, nonatomic) AppDelegate *appDelegate;

@end

Switch over to the implementation file and initialize the tableData property and the appDelegate property variable in the viewDidLoad method.

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.tableData = [NSMutableArray alloc] init];
  self.appDelegate = [[UIApplication sharedApplication] delegate];
}

Modify the Table View control’s data source methods to look like this:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  // Return the number of sections.
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  // Return the number of rows in the section.
  return self.tableData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

  // Configure the cell...
  cell.textLabel.text = self.tableData[indexPath.row];
  return cell;
}

That’s it, you are done setting up the Book List scene and it class, so run the application and you’ll see a blank Table View Control in the simulator’s window, a navigator bar with a title and a + button.