infinum ios talks s01e02 - things every ios developer should know about core data by filip beć

Post on 12-May-2015

402 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Learn about persistent store coordinator, managed object model, object contexts and other wonderful things in the Core Data iOS framework.

TRANSCRIPT

CORE DATA

WHAT IS CORE DATA?

• data persistence framework

• not a database

CORE DATA COMPONENTS

Managed object context

User application

Managed object

Persistent store coordinator

Persistent store(SQLite, XML,

in memory, custom, …)

Managed object

Managed object

Managed object model

PERSISTENT STORE• NSPersistentStore

• Types:

• SQLite

• Binary

• In memory

• Custom

PERSISTENT STORE COORDINATOR

• NSPersistentStoreCoordinator

• associates persistent object stores and a managed object model

• facade to managed object contexts

• group of persistent stores appears as a single aggregate store

PERSISTENT STORE COORDINATOR

• Complex application

MOC MOC MOC

Persistent store coordinator

Storage1 Storage2 Storage3 Storage4

MANAGED OBJECT MODEL (MOM)

• A collection of entity descriptions

• name of the class, properties (attributes and relationships)

MANAGED OBJECT CONTEXT (MOC)

• NSManagedObjectContext

• environment where managed objects live

• responsibility to manage a collection of managed objects • validation

• faulting

• (inverse) relationship handling

• undo/redo

!

• should be always accessible

MANAGED OBJECT

• NSManagedObject • model object

• represents a record from persistent store

Managed object - PersondateOfBirth 07.03.1990. firstName IvanlastName Horvat

Entity description

CORE DATA PROJECT

• INITIALISATION • persistent store coordinator • managed object model • managed object context

• HOW? • use Xcode template

CORE DATA PROJECT

• New project - Master-Detail Application

INSERTING OBJECTNSManagedObjectContext *context = … !CDLocation *location = [NSEntityDescription insertNewObjectForEntityForName:@“CDLocation” inManagedObjectContext:context]; !. . . !NSError *error = nil; if (![context save:&error]) {

// Replace this implementation with code to handle the error appropriately. NSLog(@“Error: %@“, error); }

CONTEXT HIERARCHY

MOC MOC MOC

Persistent store coordinator

MOC MOC MOC

MOC MOC• child - parent

TEMPORARY OBJECTS

• child context:

NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy; ! context.parentContext = self.managedObjectContext; // self.managedObjectContext is main context NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity]; NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context]; . . .

TEMPORARY OBJECTS

• [context save:&error] will save object to main context (parent context)

• object is still not saved to persistent store

• remove object from main context: undo or rollback

• save object in persistent store: save main context

UNDO/REDO/ROLLBACK

• NSUndoManager [context setUndoManager:undoManager]; ! [context undo]; ! [context redo];

• undo all unsaved changes [context rollback];

FETCHING OBJECTS

NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@“CDLocation" inManagedObjectContext:context]; ! [request setEntity:entity]; NSArray *locations = [context executeFetchRequest:request error:nil];

• NSFetchRequest

SORT RESULTS

• NSSortDescriptor

NSFetchRequest *request = [[NSFetchRequest alloc] init]; … … … NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"dateOfBirth" ascending:NO]; [request setSortDescriptors:@[sortDescriptor]]; !NSArray *results = [context executeFetchRequest:request error:nil];

FILTER RESULTS

• NSPredicate

NSFetchRequest *request = [[NSFetchRequest alloc] init]; … … … NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName contains %@", @"Ivana"]; [request setPredicate:predicate]; !NSArray *results = [context executeFetchRequest:request error:nil];

SEE ALSO

• NSFetchedResultController

• NSIncrementalStore

• Custom storage types

• Migrating data and versioning

LITERATURE

• Apple documentation Core Data programming Guide: https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/CoreData/cdProgrammingGuide.htmlCore Data Core Competencies: https://developer.apple.com/library/ios/documentation/DataManagement/Devpedia-CoreData/coreDataOverview.html

• Pro Core Data for iOS - second edition Michael Privat, Robert Warner

top related