Core Data
Core Data is not a Database
When we talk about persistent data, people probably think of database. If you are familiar with Oracle or MySQL, you know that relational database stores data in the form of table, row and column, and it usually facilitates access through what-so-called SQL query. However, don’t mix up Core Data with database. Though Sqlite database is the default persistent store for Core Data on iPhone, Core Data is not a relational database. It is actually a framework that lets developers store (or retrieve) data in database in an object-oriented way. With Core Data, you can easily map the objects in your apps to the table records in the database without even knowing any SQL.
To illustrate the concept, let’s begin and create your first app using Core Data. This app is called My Store. It is a very simple app that stores all devices you have by collecting the name, version,company.

MyStore App using Core Data
MyStore App using Core Data
Creating a Sample App with Core Data
First let’s create a project with Core Data. Open Xcode and create a new Project, choose the templateEmpty Application as shown below.

Create a New Project with Empty Application Template
At the next screen, enter MyStore as a name of the project, select iPhone in Devices family and don’t forget to select the options Use Storyboards, Use Core Data, Use Automatic Reference Counting. Press next and create.

Set up Xcode Project Options – Remember to select Use Core Data
Create a New Project with Empty Application Template
Set up Xcode Project Options – Remember to select Use Core Data
Defining Managed Object Model
Let’s move on to build the app. The first step is to open the Data Model named MyStore.xcdatamodeldand define the object model. Here we’ll define a Device entity that will be used to store the device information to database. To create an entity, click the + button in the bottom-left of the editor view and name the entity as Device.

Add Device entity in the model
Once you create a new entity, you need to add attributes to it. Click on the + button in the attributes section to do that. Add three attributes including name, version and company. Set the type as String.

Add 3 Attributes (company, name and version) to the Device entity
Add Device entity in the model
Add 3 Attributes (company, name and version) to the Device entity
Designing the User Interface
Note: While we encourage you to build the user interface, you can also skip the procedures anddownload the project template from here. The template already comes with the Storyboard and set up all the view controller classes for you. This gives you a good starting point to work on Core Data. If you use the template, you can skip this section and go directly to the “Diving Core Data” section.
The next thing we need to do is to create the Storyboard that defines the views of our app. Navigate to File > New > New File and choose Storyboard in the User Interface template. Click next and select the iPhone device family, click create.

Creating the Storyboard
Once created, make sure to set the “Storyboard” you’ve just created as the main storyboard in the project setting.

Set the Storyboard you just created as the Main Storyboard
Also don’t forget to delete all the generated code in the method -(BOOL)application:application didFinishLaunchingWithOptions:launchOptions inside the AppDelegate file. The method should be as simple as this:
1
2
3
4
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
Go to Storyboard and create the user interface like below:

MyStore App – Storyboard
First, drag a Table View Controller and embed it in a Navigation Controller. Drag a button to the top-right part of navigation bar and set the identifier as “Add”. This will automatically change the button to a “+” button. Next, select the prototype cell and change its style to “Right Detail”.

Creating the Table View Controller
Drag a View Controller to the Storyboard and add a Navigation Bar to the top of the screen. Next, drag two buttons into the navigation bar. Name one as “Cancel” and the other one as “Save”. In the content view, add three text fields and name the placeholder attributes as “Name”, “Version” and “Company”.
This detail view will be shown when user taps the “+” button in the table view controller. So finally, press and hold the Control key, click the “+” button and drag towards the detail view controller. Select “Modal” as the Segue action to connect the table view controller and detail view controller.

Designing the Detail View Controller
Creating the Storyboard
Set the Storyboard you just created as the Main Storyboard
1
2 3 4 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ return YES; } |
MyStore App – Storyboard
Creating the Table View Controller
Designing the Detail View Controller
Creating View Controller Classes
Create a new class by right-clicking on the MyStore folder > New File > Objective-C class, and name the class as DeviceViewController. Make it as a subclass of UITableViewController. Navigate to the Storyboard, select the Table View Controller and associate it with the DeviceViewController class.

Set the Custom Class as DeviceViewController
Once done, do the same steps to create a new class namedDeviceDetailViewControllerUIViewController. Again, go to Storyboard and set the custom class of the detail view controller as the “DeviceDetailViewController”.
Lastly, wire up the UITextFields to the DeviceDetailViewController header file and create two action methods for the save and cancel buttons respectively.

Creating IBOutlet and Action Methods
Your code should like this:
1
2
3
4
5
6
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *versionTextField;
@property (weak, nonatomic) IBOutlet UITextField *companyTextField;
- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;
Set the Custom Class as DeviceViewController
Creating IBOutlet and Action Methods
1
2 3 4 5 6 |
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *versionTextField; @property (weak, nonatomic) IBOutlet UITextField *companyTextField; - (IBAction)cancel:(id)sender; - (IBAction)save:(id)sender; |
Diving into Core Data
With the user interface, it’s time to go into the details of Core Data. Apparently, there are a couple of areas we have to implement:
- Save device information in the Detail View Controller
- Fetch device information from persistent store (i.e. SQLite database) and populate the data into Table View Controller
We’ll look into the implementation one by one.
Then Finally Write Code Which Is Given Below:
AppDelegate.h :
#import <UIKit/UIKit.h>
#import "DeviceViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(strong,nonatomic)UINavigationController *navobj;
@property(strong,nonatomic)DeviceViewController *devobj;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
AppDelegate.m :
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize navobj,devobj;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
devobj = [story instantiateViewControllerWithIdentifier:@"DeviceViewController"];
navobj = [[UINavigationController alloc] initWithRootViewController:devobj];
self.window.rootViewController = navobj;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Storyboard.storyboard :
DeviceViewController.h :
#import <UIKit/UIKit.h>
@interface DeviceViewController : UIViewController
@property(strong,nonatomic)IBOutlet UITableView *tblobj;
@property(strong,nonatomic)NSMutableArray *devarr;
@end
DeviceViewController.m :
#import "DeviceViewController.h"
#import "detailViewController.h"
@interface DeviceViewController ()
@end
@implementation DeviceViewController
@synthesize tblobj,devarr;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)])
{
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
devarr = [[NSMutableArray alloc] init];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
self.devarr = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tblobj reloadData];
}
- (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.devarr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *device = [self.devarr objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [device valueForKey:@"name"], [device valueForKey:@"version"]]];
[cell.detailTextLabel setText:[device valueForKey:@"company"]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.devarr objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
// Remove device from table view
[self.devarr removeObjectAtIndex:indexPath.row];
[self.tblobj deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"UpdateDevice"])
{
NSManagedObject *selectedDevice = [self.devarr objectAtIndex:[[self.tblobj indexPathForSelectedRow] row]];
detailViewController *destViewController = segue.destinationViewController;
destViewController.device = selectedDevice;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
detailViewController.h :
#import <UIKit/UIKit.h>
@interface detailViewController : UIViewController
@property(strong,nonatomic)NSManagedObject *device;
@property (strong, nonatomic) IBOutlet UITextField *txtname;
@property (strong, nonatomic) IBOutlet UITextField *txtversion;
@property (strong, nonatomic) IBOutlet UITextField *txtcompany;
- (IBAction)savedata:(id)sender;
- (IBAction)cancel:(id)sender;
@end
detailViewController.m :
#import "detailViewController.h"
@interface detailViewController ()
@end
@implementation detailViewController
@synthesize device;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.device)
{
[self.txtname setText:[self.device valueForKey:@"name"]];
[self.txtversion setText:[self.device valueForKey:@"version"]];
[self.txtcompany setText:[self.device valueForKey:@"company"]];
}
}
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)])
{
context = [delegate managedObjectContext];
}
return context;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)savedata:(id)sender
{
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new managed object
// NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
// [newDevice setValue:self.txtname.text forKey:@"name"];
// [newDevice setValue:self.txtversion.text forKey:@"version"];
// [newDevice setValue:self.txtcompany.text forKey:@"company"];
if (self.device)
{
// Update existing device
[self.device setValue:self.txtname.text forKey:@"name"];
[self.device setValue:self.txtversion.text forKey:@"version"];
[self.device setValue:self.txtcompany.text forKey:@"company"];
}
else
{
// Create a new device
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
[newDevice setValue:self.txtname.text forKey:@"name"];
[newDevice setValue:self.txtversion.text forKey:@"version"];
[newDevice setValue:self.txtcompany.text forKey:@"company"];
}
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)cancel:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end

No comments:
Post a Comment