0% found this document useful (0 votes)
35 views42 pages

Cored Ata: Omega Team

This document provides an overview of Core Data and discusses concurrency issues and solutions when working with Core Data across multiple threads. It begins with definitions of Core Data and its main functions like object graph management and persistence. It then explains the concurrency problems that arise from Core Data objects not being thread-safe. Several solutions for managing concurrency are presented, including traditional multi-context with one context per thread, and parent-child contexts introduced in iOS 5 which use Grand Central Dispatch to manage threading. The benefits and limitations of each approach are discussed.

Uploaded by

Lê Văn Toàn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views42 pages

Cored Ata: Omega Team

This document provides an overview of Core Data and discusses concurrency issues and solutions when working with Core Data across multiple threads. It begins with definitions of Core Data and its main functions like object graph management and persistence. It then explains the concurrency problems that arise from Core Data objects not being thread-safe. Several solutions for managing concurrency are presented, including traditional multi-context with one context per thread, and parent-child contexts introduced in iOS 5 which use Grand Central Dispatch to manage threading. The benefits and limitations of each approach are discussed.

Uploaded by

Lê Văn Toàn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

CoreD ata

OMEGA Team

Roadmap
Coredata
Multithread
Memory Management
Notification
MVC
OOP
Introduction

Roadmap
Coredata
Multithread
Memory Management
Notification
MVC
OOP
Introduction

Agenda
Core D ata basic
Concurrency problem s
Concurrency solutions

Agenda
Core D ata basic
Concurrency problem s
Concurrency solutions

W hat is Core D ata?


Core D ata is a very im portant subfram ew ork of the C ocoa
fram ew ork that provides an O RM for O bjective-C and SQ Lite
Its not just an O RM , it m ay look like theres SQ L under the hood
Available both on the desktop and on the iPhone O S
Functionality to create, update, delete, query
M ultiple back-end storage options

W hat is Core D ata?


Core D ata provides:
Persistence
Change tracking
Relations (object graph)
Lazy loading (faulting)
Validation
W ork w ellw ith KVO (key-value observing), KVC (key-value
coding)

Basically
A system to store data
Persistence agnostic (localstorage)
N o need to w rite SQ L to query
You can keep to O bjective C

Storage O ptions
Binary fi
le
In-m em ory
XM L
N ot available on the iPhone
SQ Lite
Custom

Core D ata vs. SQ Lite


SQLite

Core
Data

Bulk/Set
Operatio
ns

Yes

No

Automati
c object
populatio
n

No

Yes

Custom
SQL

Yes

No

Select
only
certain

Yes

No

Your tools

N SM anagedO bjectM odel


N SPersistentStoreCoordinator
N SM anagedO bjectContext
N SM anagedO bject
N SM anagedO bjectID
N SFetchRequest
N SEntityD escription
N SPredicate
N SSortD escription
N SFetchedResultsController

A picture says 1000 w ords...

A picture says 1000 w ords...

M anaged O bject M odel

M anaged O bject M odel


- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"core-data"
withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: modelURL];
return _managedObjectModel;
}

Persistent Store Coordinator

Persistent Store Coordinator


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@core-data.sqlite];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self
managedObjectModel]];
if(![_persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType
configuration: nil
URL: storeURL
options: nil
error: &error]) {
NSLog(@Unresolved error %@, %@, error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;

Persistent Store Coordinator


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@core-data.sqlite];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self
managedObjectModel]];
if(![_persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType
configuration: nil
URL: storeURL
options: nil
error: &error]) {
NSLog(@Unresolved error %@, %@, error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;

M anaged O bject Context

M anaged O bject Context


- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if(coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return _managedObjectContext;
}

Single Context

Agenda
Core D ata basic
Concurrency problem s
Concurrency solutions

Problem s
Core D ata M anaged O bjects are not thread safe
M ust pass O bject ID s to use across threads
O bjects are locked for alloperations including read
O bjects that feed the U Im ust be fetched on the m ain thread

Agenda
Core D ata basic
Concurrency problem s
Concurrency solutions

TraditionalM ulti-Context
Pre-iO S 5: Thread Confi
n em ent
Single N SM anagedO bjectContext per thread
M anualnotifi
cations, m erging, and saving
Fairly easy to understand, but harder to m anage

TraditionalM ulti-Context

Parent Child Context


iO S 5+ : Parent Child Contexts
G rand CentralD ispatch private dispatch queues
Threading m anaged for you, no m anualsynchronization
required
Less com plicated and m ore fl
exible than pre-iO S 5 m ethod
Context can and should be nested

Parent Child Context

Parent Child Context

Parent Child Context

Parent Child Context

Parent Child Context

Problem s - Parent Child Context

Async Saving - Parent Child Context

Async Saving - Parent Child Context

Async Saving - Parent Child Context

Async Saving - Parent Child Context

Async Saving - Parent Child Context

Async Saving - Parent Child Context

Stillblocking?

Stillblocking?

For V ER Y LA R G E am ounts of data it m ay be better to generate the


SQ Lite fi
le on the server, dow nload it asynchronously, and set it up
an additionalpersistent store.

QA

You might also like