Introduction To Core Data

3,463 views

Published on

A small and quick introduction to Core Data on the iPhone.

Published in: Technology, Business
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
3,463
On SlideShare
0
From Embeds
0
Number of Embeds
54
Actions
Shares
0
Downloads
106
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Introduction To Core Data

  1. 1. Introduction to Core Data Daniel Tull
  2. 2. Sky+ Little Spender Weather Maps Fourgy
  3. 3. Core Data Stack NSManagedObjectModel
  4. 4. Managed Object Model
  5. 5. Attributes NSNumber NSString Transformable BOOL You put in code to transform the NSDate object or value into NSData a compatible type. Transformable
  6. 6. 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
  7. 7. Relationships DTAlbum Attributes DTArtist name DTGenre Attributes discCount Attributes name trackCount name Relationships Relationships Relationships albums artist artists genres composers songs songs songs
  8. 8. Core Data Stack NSManagedObjectContext NSPersistentStoreCoordinator NSPersistentStore NSManagedObjectModel
  9. 9. 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];
  10. 10. Create and Save Objects NSManagedObjectContext DTArtist DTSong NSPersistentStoreCoordinator NSPersistentStore NSManagedObjectModel
  11. 11. Fetching Objects All songs by “La Roux” sorted by title. Entity DTSong Predicate @“artist.name == %@”, @“La Roux” Sorting @“title”
  12. 12. Fetching Objects Fetch all playlists which contain more than an hour of music. Entity DTPlaylist Predicate @“@sum.songs.duration > 3600” Sorting @“name”
  13. 13. 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];
  14. 14. Fetching Objects NSManagedObjectContext NSFetchRequest DTArtist DTSong DTSong DTSong DTSong NSPersistentStoreCoordinator NSPersistentStore DTSong NSManagedObjectModel DTSong DTSong
  15. 15. Performance: Faults Faults are placeholders for related objects, which mean you don’t use memory on unwanted objects. DTSong DTSong DTArtist DTSong DTSong DTSong
  16. 16. Performance: Batching DTArtist DTArtist Batching allows you to bring only a DTArtist subset of objects DTArtist into memory. DTArtist DTArtist
  17. 17. Demo
  18. 18. DTMusicModel hg.danieltull.co.uk/dtmusicmodel
  19. 19. DTGridView DTSwapView hg.danieltull.co.uk/dtkit
  20. 20. Thank You dt@danieltull.co.uk hg.danieltull.co.uk

×