CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational...

31
CS 696 Mobile Application Development Fall Semester, 2010 Doc 16 Core Data Oct 28, 2010 Copyright ©, All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http:// www.opencontent.org/openpub/) license defines the copyright on this document.

Transcript of CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational...

Page 1: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

CS 696 Mobile Application DevelopmentFall Semester, 2010Doc 16 Core Data

Oct 28, 2010

Copyright ©, All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/openpub/) license defines the copyright on this document.

Page 2: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

O-R Mapping - Vietnam of Computer Science

2

http://blogs.tedneward.com/2006/06/26/The+Vietnam+Of+Computer+Science.aspx

http://www.codinghorror.com/blog/archives/000621.html

Dual-Schema ProblemEntity Identity IssuesThe Data Retrieval Mechanism

Query-By-Example (QBE)Query-By-API (QBA)Query-By-Language (QBL)

Last mile problem & OR mapping Problem

Page 3: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Core Data

3

Object-relational mapping layer

Object-graph management and persistence framework

Makes it easy to save & load objects

Supports undo/redo

Higher level than SQLite

Claim 50% to 70% less code

On Mac OS & iOS

Page 4: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Entities & Attributes

4

Entities (table)Like class with only instance variablesDefine data for managed objects

Attribute (column)Like field in classBasic types

Int, decimal, float, bool, date, binaryCan use other types (with more work)

Page 5: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Relationships

5

Entity uses other entity

(Foreign key)

to manyto one

Normally define relationship in both directions

When load object also load its relationships

Page 6: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Fetched Properties

6

(Foreign key, lazy load)

Property is loaded only when needed

Page 7: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Behavior

7

Each entity has a classDefault is NSManagedObject

If need behavior subclass NSManagedObject

Xcode will generate classes for you with accessors

Page 8: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Defining Entities

8

Page 9: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Terms

9

Managed objectInstance of an entity

Persistent (object) storeHas file to store objectsMaps between objects & file storageFile storage type can be changed

Persistent store coordinatorMakes multiple stores appear as one store

Fetch requestsHow to request objects from a persistent store

Page 10: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Terms

10

Managed object context

Intelligent scratch pad

Keeps fetched objects

Track changes for undo

Changes temporary until objects saved

Validates object state before saving

Page 11: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Configuring Project for Core Data

11

Page 12: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

What we get in App Delegate

12

NSManagedObjectContextNSManagedObjectModel NSPersistentStoreCoordinator

Auto-generated code to access

Page 13: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

What we get in App Delegate

13

@interface YourClassName : NSObject <UIApplicationDelegate> { UIWindow *window; @private NSManagedObjectContext *managedObjectContext_; NSManagedObjectModel *managedObjectModel_; NSPersistentStoreCoordinator *persistentStoreCoordinator_;}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator

*persistentStoreCoordinator;

Page 14: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Example

14

Person Entityage - intname - string

Create & SaveReadDelete

Page 15: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Auto generated Class for Person Entity

15

#import <CoreData/CoreData.h>

@interface Person : NSManagedObject {}

@property (nonatomic, retain) NSString * name;@property (nonatomic, retain) NSNumber * age;

@end

#import "Person.h"

@implementation Person

@dynamic name;@dynamic age;

@end

Page 16: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Class for Entity

16

Can treat entity as a class

Allows us to use standard accessor methods

person.name = @"Roger";[person setName: @"Roger";

rather than key-value access[person setValue: @"Roger forKey: @"name"];

Property access is slightly faster

Can add logic to class

Page 17: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Creating new person in controller

17

- (IBAction) newPerson { NSManagedObjectContext *context = [self managedObjectContext]; Person *person = (Person *)[NSEntityDescription

insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];

person.name = name.text; person.age = [NSNumber numberWithInt: [age.text integerValue]];

NSError *error; if (![context save:&error]) { NSLog(@"Error on save: %@", error); }}

Page 18: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Int Attributes

18

person.age = [NSNumber numberWithInt: [age.text integerValue]];

Stored as NSNumber

Page 19: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

19

- (NSManagedObjectContext *) managedObjectContext { Core_Data_PersistenceAppDelegate *appDelegate =

[[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; return context;}

Page 20: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Reading all

20

- (IBAction) readPerson { NSManagedObjectContext *context = [self managedObjectContext]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person"

inManagedObjectContext:context]; [request setEntity:entity]; NSError *error = nil; NSMutableArray *people = [[context executeFetchRequest:request error:&error]

mutableCopy]; if (people == nil) { // Handle the error. } Person * first = [people objectAtIndex:0]; name.text = first.name; age.text = [first.age stringValue];}

Page 21: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Delete

21

NSManagedObjectContext *context = [self managedObjectContext];

Person * toDelete = code to select person to delete;

[context deleteObject:toDelete];

NSError *error; if (![context save:&error]) { NSLog(@"Error on save: %@", error); }

Page 22: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Changing data

22

NSManagedObjectContext *context = [self managedObjectContext];

Person * toChange = code to get person to change; toChange.name = @"mud";

NSError *error; if (![context save:&error]) { NSLog(@"Error on save: %@", error); }

Page 23: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Undo

23

NSManagedObjectContext *context = [self managedObjectContext]; Person * a = code to get a; Person * b = get b; a.name = @"foo"; b.name = @"bar"; a.name = @"foofoo"; [context undo]; [context redo]; [context undo]; [context rollback]; [context redo];

a b

foo b

foo bar

foofoo bar

a b

foofoo bar

a b

a b

foofoo bar

Page 24: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Fine grain undo

24

Need NSUndoManager

NSUndoManager has undo groups (regions)

undoends the current undo groupUndoes all operations in previous undo groupPlaces group in redo stack

redo

Does all operations in the top undo group in redo stack

rollbackRemoved everything from undo stackUpdates objects to their last commited (saved) state

Page 25: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

In App Delegate

25

- (NSManagedObjectContext *) managedObjectContext { if (managedObjectContext != nil) { return managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { managedObjectContext = [[NSManagedObjectContext alloc] init]; NSUndoManager *anUndoManager = [[NSUndoManager alloc] init]; [managedObjectContext setUndoManager:anUndoManager]; [anUndoManager release]; [managedObjectContext setPersistentStoreCoordinator: coordinator]; } return managedObjectContext;}

in iOS there is no default NSUndoManager

Page 26: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Fine grain undo

26

NSManagedObjectContext *context = [self managedObjectContext]; Person * a = code to get a; Person * b = code to get b; //a.name ==a, b.name == b NSUndoManager * undoManager = [context undoManager]; a.name = @"foo"; //a.name==foo, b.name== b [undoManager endUndoGrouping]; [undoManager beginUndoGrouping]; b.name = @"bar"; //a.name==foo, b.name== bar [undoManager endUndoGrouping]; [undoManager beginUndoGrouping]; a.name = @"foofoo"; //a.name==foofoo, b.name== bar [undoManager endUndoGrouping]; [context undo]; //a.name==foo, b.name==bar [context undo]; //a.name==foo, b.name==b [context undo]; //a.name==a, b.name==b [context redo]; //a.name==foo, b.name==b [context redo]; //a.name==foo, b.name==bar [undoManager beginUndoGrouping]; [context rollback]; //a.name==a, b.name==b

Page 27: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Making Queries

27

Use NSPredicate with string queries

Generate queries directly using NSComparisonPredicate NSCompoundPredicate

Page 28: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Sample Query with order

28

NSManagedObjectContext *context = [self managedObjectContext]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person"

inManagedObjectContext:context]; [request setEntity:entity]; NSNumber *minumumAge = [NSNumber numberWithInt:5]; NSPredicate *selectOldRoger = [NSPredicate predicateWithFormat:

@"(name LIKE[c] 'Roger') AND (age > %@)", minumumAge]; [request setPredicate:selectOldRoger]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [request setSortDescriptors:sortDescriptors]; [sortDescriptor release]; [sortDescriptors release]; NSError *error = nil; NSMutableArray *people = [[context executeFetchRequest:request error:&error] mutableCopy]; if (people == nil) { // Handle the error. }

Page 29: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Query String format

29

Basic Comparisons=,==>=,=><=, =<><!=, <>BETWEEN

Compound PredicatesAND, &&OR, ||NOT, !

String ComparisonsBEGINSWITHCONTAINSENDSWITHLIKE

*, ? - wildcard charactersMATCHES

regular expressionsAggregate OperationsAND, SOMEALLNONEINarray[index]array[FIRST]array[LAST]

See Predicate Programming Guide chapter 6 Predicate Format String Syntax for more details

Page 30: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Managed Object IDs and URIs

30

NSManagedObjectID *moID = [managedObject objectID];

Each object has unique IDID is temporay until object is saved

Can be used to read the objectUseful in restoring state of application

[context objectWithID: moID]

Page 31: CS 696 Mobile Application Development Fall Semester, 2010 ... · Core Data 3 Object-relational mapping layer Object-graph management and persistence framework Makes it easy to save

Versions & Migrations

31

Core Data Supports Different versions of your dataMigrating from old version of data to new version