Top Banner
Introduction to Core Data Daniel Tull
20

Introduction To Core Data

May 12, 2015

Download

Technology

danielctull

A small and quick introduction to Core Data on the iPhone.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction To Core Data

Introduction toCore Data

Daniel Tull

Page 2: Introduction To Core Data

Sky+

Weather Maps

Little Spender

Fourgy

Page 3: Introduction To Core Data

Core Data Stack

NSManagedObjectModel

Page 4: Introduction To Core Data

Managed Object Model

Page 5: Introduction To Core Data

Attributes

NSNumber

NSString

BOOL

NSDate

NSData

Transformable

Transformable

You put in code to transform the object or value into a compatible type.

Page 6: Introduction To Core Data

Relationships

Properties that reference other objects

Can be to-one or to-many (NSSet - unordered)

Can (and should) have inverses, which are worked out by Core Data when you set the other property

Delete rule ensures propagation

Page 7: Introduction To Core Data

Relationships

DTGenreAttributes

nameRelationships

artistssongs

DTArtistAttributes

name

Relationshipsalbumsgenressongs

DTAlbum

AttributesnamediscCounttrackCount

Relationshipsartistcomposerssongs

Page 8: Introduction To Core Data

Core Data Stack

NSPersistentStore

NSManagedObjectModel

NSManagedObjectContext

NSPersistentStoreCoordinator

Page 9: Introduction To Core Data

Create and Save Objects

DTSong *song = [NSEntityDescription insertNewObjectForEntityForName:@“DTSong” inManagedObjectContext:managedObjectContext];

DTArtist *artist = [NSEntityDescription insertNewObjectForEntityForName:@“DTArtist” inManagedObjectContext:managedObjectContext];

artist.name = @“Moby”;

song.title = @“Porcelain”;

song.artist = artist;

•••

NSError *error = nil;[managedObjectContext save:&error];

Page 10: Introduction To Core Data

Create and Save Objects

NSPersistentStore

NSManagedObjectModel

NSManagedObjectContext

NSPersistentStoreCoordinator

DTArtist

DTSong

DTArtist

DTSong

DTArtist

DTSong

Page 11: Introduction To Core Data

Fetching Objects

Entity

Predicate

Sorting

DTSong

@“artist.name == %@”, @“La Roux”

@“title”

All songs by “La Roux” sorted by title.

Page 12: Introduction To Core Data

Fetching Objects

Entity

Predicate

Sorting

DTPlaylist

@“@sum.songs.duration > 3600”

@“name”

Fetch all playlists which contain more than an hour of music.

Page 13: Introduction To Core Data

Fetching Objects

NSEntityDescription *entity = [NSEntityDescription entityForName:@“DTSong” inManagedObjectContext:managedObjectContext];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@“artist.name == %@”, @“La Roux”];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@“title” ascending:YES];

NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];

NSFetchRequest *request = [[NSFetchRequest alloc] init];[request setEntity:entity];[request setPredicate:predicate];[request setSortDescriptors:sortDescriptors];

NSArray *fetchResult = [managedObjectContext executeFetchRequest:request error:&error];

Page 14: Introduction To Core Data

NSFetchRequest

Fetching Objects

NSPersistentStore

NSManagedObjectModel

NSManagedObjectContext

NSPersistentStoreCoordinator

DTArtist DTSongDTSongDTSong

DTSongDTSongDTSong

DTSong

Page 15: Introduction To Core Data

Performance: Faults

Faults are placeholders for related objects, which mean you don’t use memory on unwanted objects.

DTArtist

DTSongDTSongDTSongDTSongDTSong

Page 16: Introduction To Core Data

Performance: Batching

Batching allows you to bring only a subset of objects into memory.

DTArtist

DTArtist

DTArtist

DTArtist

DTArtist

DTArtist

Page 17: Introduction To Core Data

Demo

Page 18: Introduction To Core Data

hg.danieltull.co.uk/dtmusicmodel

DTMusicModel

Page 19: Introduction To Core Data

DTSwapView

hg.danieltull.co.uk/dtkit

DTGridView

Page 20: Introduction To Core Data

Thank [email protected]

hg.danieltull.co.uk