Your SlideShare is downloading. ×
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Going Down the Core Data Rabbit Hole
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Going Down the Core Data Rabbit Hole

1,931

Published on

Core Data is a great framework that Apple provides to help persist data in your applications. If you have familiarity with other persistence frameworks like N/Hibernate and Entity Framework then you …

Core Data is a great framework that Apple provides to help persist data in your applications. If you have familiarity with other persistence frameworks like N/Hibernate and Entity Framework then you may feel right at home with Core Data! Using SQLite directly can be a real pain, especially if you have little to no C experience. Core Data lets you ignore the actual store used and gives a great interface to make saving information a little more straightforward. This session will cover the basics and intermediate topics of Core Data and some of the bigger gotchas that Apple's Documentation doesn't necessarily call out. I'll give some of the tips and tricks I've learned over the years of using Core Data so that you have less pain to get started. Core Data is certainly not the most friendly of APIs, but once you learn it, you'll use it all the time.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,931
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Going Down the Core Data Rabbit Hole Aaron Douglas @astralbodies 1Tuesday, August 13, 13
  • 2. Platinum Sponsors Gold Sponsors 2Tuesday, August 13, 13
  • 3. Who am I? ✦ Aaron Douglas ✦ @astralbodies ✦ Automattic (WordPress) ✦ iOS Developer / Android (a little) ✦ Previous Life == Enterprise Java Developer 3Tuesday, August 13, 13
  • 4. Who are you? ✦ iOS Developers? Tinkerers? ✦ Anyone use SQLite before? ✦ How about Core Data? 4Tuesday, August 13, 13
  • 5. SQLite is GREAT ✦ Self-contained ✦ Server-less ✦ Nearly zero configuration ✦ Transactional ✦ Written in C / Super portable 5Tuesday, August 13, 13
  • 6. SQLite meh ✦ Written in C ✦ No ARC ✦ Foundation wrappers ✦ Lots of boilerplate Code ✦ Room for archive/unarchive mapping errors 6Tuesday, August 13, 13
  • 7. Why Core Data? ✦ Lets Step Back 7Tuesday, August 13, 13
  • 8. ORM ✦ Object Relational Mapping ✦ Maps tables to real objects ✦ Persisting is relatively easy ✦ Less code (usually) ✦ Hibernate, Entity Framework, Ibatis 8Tuesday, August 13, 13
  • 9. Why Core Data? ✦ In Appleland for many years - OS X Tiger, iPhone OS 3.0 ✦ Xcode visual designer ✦ Validation ✦ Migration ✦ Persist anywhere - SQLite, XML, memory 9Tuesday, August 13, 13
  • 10. Core Data Topics ✦ Main moving parts ✦ Managed Object ✦ Managed Object Model & Context ✦ Persistent Store ✦ Persistent Store Coordinator ✦ Fetch Requests 10Tuesday, August 13, 13
  • 11. Managed Objects 11Tuesday, August 13, 13
  • 12. Models ✦ NSManagedObjectModel ✦ Designer in Xcode ✦ Through code 12Tuesday, August 13, 13
  • 13. Managed Objects ✦ objectID ✦ NSString, NSDate, NSNumber ✦ Validation ✦ Undo Management ✦ Faults ✦ Refresh 13Tuesday, August 13, 13
  • 14. Object Contexts ✦ Scratchpad ✦ Each object associated to single context ✦ Objects are not transferrable ✦ Nested contexts 14Tuesday, August 13, 13
  • 15. Operations ✦ object = [NSManagedObject insertNewObjectForEntityForName:@”Speak er” inManagedObjectContext:context]; ✦ [context deleteObject:object]; 15Tuesday, August 13, 13
  • 16. Relationships ✦ NSSet ✦ NSOrderedSet ✦ Deletes 16Tuesday, August 13, 13
  • 17. Finding Objects ✦ NSFetchRequest ✦ NSPredicate ✦ NSSortDescriptor 17Tuesday, August 13, 13
  • 18. Fetching Example NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Appointment" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; request.entity = entityDescription; NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"(date >= %@)", startDate]; NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"(date <= %@)", endDate]; NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"salesRep == %@", [self.salesRepService getSalesRepByGeneracId:salesRepId]]; NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"active == %@", @YES]; NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate1, predicate2, predicate3, predicate4]]; request.predicate = predicate; NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]; NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"slotNumber" ascending:YES]; request.sortDescriptors = @[sortDescriptor1, sortDescriptor2]; NSArray *data = [context executeFetchRequest:request error:&error]; 18Tuesday, August 13, 13
  • 19. Fetching Example NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Appointment" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; request.entity = entityDescription; ... 19Tuesday, August 13, 13
  • 20. Fetching Example ... NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"(date >= %@)", startDate]; NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"(date <= %@)", endDate]; NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"salesRep == %@", salesRepId]; NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"active == %@", @YES]; NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate1, predicate2, predicate3, predicate4]]; request.predicate = predicate; ... 20Tuesday, August 13, 13
  • 21. Fetching Example ... NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]; NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"slotNumber" ascending:YES]; request.sortDescriptors = @[sortDescriptor1, sortDescriptor2]; ... 21Tuesday, August 13, 13
  • 22. Fetching Example ... NSArray *data = [context executeFetchRequest:request error:&error]; ... 22Tuesday, August 13, 13
  • 23. Performance ✦ NSFetchedResultsController ✦ Speeds up UITableView reloads & scrolling ✦ Watches for changes ✦ Reduces memory overhead 23Tuesday, August 13, 13
  • 24. Persistent Stores 24Tuesday, August 13, 13
  • 25. Persistent Stores ✦ NSPersistentStoreCoordinator ✦ Persisting, Loading, Caching Data ✦ Incremental Stores 25Tuesday, August 13, 13
  • 26. Store Types ✦ NSPersistentStore ✦ XML (not on iOS) ✦ Atomic ✦ SQLite ✦ In-Memory 26Tuesday, August 13, 13
  • 27. Validation 27Tuesday, August 13, 13
  • 28. Core Data Validation ✦ Models have basic validation rules ✦ Single property validation ✦ Inter-property validation ✦ Multiple errors 28Tuesday, August 13, 13
  • 29. Custom Validation ✦ validate<Key>:error: method in NSManagedObject subclass ✦ Do not call directly ✦ Common place for error codes/states 29Tuesday, August 13, 13
  • 30. Inter-Property ✦ validateForInsert: validateForUpdate: overrides ✦ Make sure to call super ✦ Return NO if invalid; short-circuit if super is NO ✦ Easy to mess up! 30Tuesday, August 13, 13
  • 31. Migration 31Tuesday, August 13, 13
  • 32. Migrations ✦ Changing data models affect users new and existing ✦ Need a consistent state of data ✦ Defaults ✦ Field changes 32Tuesday, August 13, 13
  • 33. Automatic Migration ✦ It’s MAGIC, right? ✦ Understand the issues 33Tuesday, August 13, 13
  • 34. App Version Model Version 1.0 1 1.1 1 2.0 2 3.0 3 34Tuesday, August 13, 13
  • 35. App Version Model Version 1.0 1 3.0 3 NSInferMappingModelAutomaticallyOption 35Tuesday, August 13, 13
  • 36. Inferred Limitations ✦ Add & Remove Attributes ✦ Non-optional becomes optional ✦ Optional becomes non-optional with default ✦ Renaming entity or property 36Tuesday, August 13, 13
  • 37. Manual Mapping ✦ More complex scenarios ✦ Code is needed for sequential migrations 37Tuesday, August 13, 13
  • 38. Take-Aways ✦ Test migrations!! ✦ Keep old app binaries ✦ Pluck sqlite file for testing 38Tuesday, August 13, 13
  • 39. Multi-Threading 39Tuesday, August 13, 13
  • 40. Thread Confinement ✦ Separate MO Context for each thread ✦ Share Persistent Store Coordinator ✦ Pass object IDs rather than objects ✦ Track changes with notifications 40Tuesday, August 13, 13
  • 41. Undo Manager 41Tuesday, August 13, 13
  • 42. Undo Management ✦ NSUndoManager ✦ Built-in support in NSManagedObjectContext ✦ Undo manager is nil in iOS ✦ Simple to use, easy to mess up 42Tuesday, August 13, 13
  • 43. Usage NSUndoManager *undoManager = [[NSUndoManager alloc] init]; undoManager.levelsOfUndo = 10; context.undoManager = undoManager; ... [context.undoManager undo]; 43Tuesday, August 13, 13
  • 44. Demo 44Tuesday, August 13, 13
  • 45. References ✦ Core Data Programming Guide - Apple https://developer.apple.com/library/ios/ documentation/Cocoa/Conceptual/CoreData/ ✦ Core Data 2nd Ed - Marcus Zarra 45Tuesday, August 13, 13
  • 46. Questions? 46Tuesday, August 13, 13

×