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.
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
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
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
32. Migrations
✦ Changing data models affect users new and
existing
✦ Need a consistent state of data
✦ Defaults
✦ Field changes
32Tuesday, 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
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
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