Core Data on iOS
description
Transcript of Core Data on iOS
Core Data on iOS
Dhwanil Karwa
Topics
• What is Core Data?
• Framework Overview
• Using Core Data
• Demo
1
What is Core Data?• It is not a database.
• It is not an Object Relational Mapping framework.
• Apple explains it as “Core Data is a Schema driven object graph management and persistence Framework”
• It creates a layer between database and UI.
1
• Performs CRUD operations
• Queries values using NSPredicate
• You don’t define the identity primary keys, only give relationships.
What is Core Data?
• Before Core Data, developers had to work with Sqlite3 to save data.
• Handling CRUD operations was difficult.
• User was responsible to check if saved data matched the schema.
• Means you are writing your own persistence Layer
• Using Core Data hides Sqlite3
Why learn Core Data?
Storage Options
• Binary
• In-memory
• XML only on Mac OS X
• Sqlite
SQLITE CORE DATA
Bulk Operations YES NO
Automatic Object Population NO YES
Custom SQL YES NO
Select certain fields YES NO
Automatic Migration NO YES
Less Complex Code NO YES
Core Data vs. SQLite3
Core Data M-VC
Controller View
Model
Core Data
ImplementsObserver
Controller implements Strategy for the View
Core Data Stack
Heart of Core Data
• NSManagedObjectModel
• NSPersistentStoreCoordinator
• NSManagedObjectContext
NSManagedObjectModel
• Schema for Models
• Entity, Attributes, Relationships
• Fetch Requests
• Validation
NSManagedObjectModel
• Integer, Decimal, Float, Double
• Boolean
• Date
• String
• Binary Data
• Transformable
Attribute Types
NSManagedObjectModel
• NSSet not NSArray
• One to One
• One to Many
• Many to Many
• Delete Rules
• Nullify, Cascade, Deny
Relationships
NSManagedObjectModel
NSPersistentStoreCoordinator• Coordinates access to one or more
persistent stores.
• Depends on Managed Object Model
• Set store options
NSPersistentStoreCoordinator
NSManagedObjectContext
Core Data Model Inspector
Core Data Model Inspector
Save Objects
NSFetchRequest
DEMO
References
• Apple Developer. 2012. Core Data Programming Guide
https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/cdProgrammingGuide.html#//apple_ref/doc/uid/TP30001200-SW1
• Burkepile Adam. 2013.http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started
Questions?
Thank You