Presentation-Core Data

Download Report

Transcript Presentation-Core Data

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
What is Core Data?
•
Performs CRUD operations
•
Queries values using NSPredicate
•
You don’t define the identity primary keys, only give
relationships.
Why learn 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
Storage Options
•
Binary
•
In-memory
•
XML only on Mac OS X
•
Sqlite
Core Data vs. SQLite3
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 M-VC
Controller implements Strategy
for the View
View
Controller
Model
Core Data
Implements
Observer
Core Data Stack
Heart of Core Data
• NSManagedObjectModel
• NSPersistentStoreCoordinato
r
• NSManagedObjectContext
NSManagedObjectModel
•
Schema for Models
•
Entity, Attributes, Relationships
•
Fetch Requests
•
Validation
NSManagedObjectModel
Attribute Types
•
Integer, Decimal, Float, Double
•
Boolean
•
Date
•
String
•
Binary Data
•
Transformable
NSManagedObjectModel
Relationships
•
NSSet not NSArray
•
One to One
•
One to Many
•
Many to Many
•
Delete Rules
•
Nullify, Cascade, Deny
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.htm
l#//apple_ref/doc/uid/TP30001200-SW1
•
Burkepile Adam. 2013.
http://www.raywenderlich.com/934/core-data-tutorialfor-ios-getting-started
Questions?
Thank You