Top Banner
These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 214 Core Data Best Practices Ben Trumbull Core Data Engineering Manager
147
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Session 214 Core Data Best Practices

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

Session 214

Core Data Best Practices

Ben TrumbullCore Data Engineering Manager

Page 2: Session 214 Core Data Best Practices

Today’s Roadmap

• Concurrency•Nested Contexts• Performance• Schema Design• Search Optimization

Page 3: Session 214 Core Data Best Practices

Topics

•Using Core Data with multiple threads• Sharing unsaved changes between contexts•Debugging performance with Instruments• Tuning your model• Improving your predicates

Page 4: Session 214 Core Data Best Practices

Concurrency

Page 5: Session 214 Core Data Best Practices

Challenges

• Thread safety• Transactionality• Performance

Page 6: Session 214 Core Data Best Practices

NSManagedObjectContext Concurrency

Page 7: Session 214 Core Data Best Practices

[moc performSelectorOnMainThread: @selector(mergeChangesFromContextDidSaveNotification:) withObject:note waitUntilDone:NO];

NSManagedObjectContext Concurrency

Page 8: Session 214 Core Data Best Practices

NSManagedObjectContext Concurrency

• Block supportNSManagedObjectContext -performBlock:NSManagedObjectContext -performBlockAndWait:

Page 9: Session 214 Core Data Best Practices

NSManagedObjectContext Concurrency

• Block supportNSManagedObjectContext -performBlock:NSManagedObjectContext -performBlockAndWait:

[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainThreadConcurrencyType];[moc performBlock:^{ [moc mergeChangesFromContextDidSaveNotification:aNotification];}];

Page 10: Session 214 Core Data Best Practices

Core Data Concurrency Options

NSManagedObjectContext -initWithConcurrencyType:

Page 11: Session 214 Core Data Best Practices

Core Data Concurrency Options

NSManagedObjectContext -initWithConcurrencyType:

User Interface Elements

Main Thread

NSMainQueueConcurrencyType

Page 12: Session 214 Core Data Best Practices

Core Data Concurrency Options

NSManagedObjectContext -initWithConcurrencyType:

User Interface Elements

Main Thread Private Queue

Managed Objects

NSMainQueueConcurrencyType NSPrivateQueueConcurrencyType

Page 13: Session 214 Core Data Best Practices

Core Data Concurrency Options

NSManagedObjectContext -initWithConcurrencyType:

User Interface Elements

Main Thread Private Queue

Managed Objects

Developer Queue

Anything

NSMainQueueConcurrencyType NSPrivateQueueConcurrencyType NSConfinementConcurrencyType

Page 14: Session 214 Core Data Best Practices

NSConfinementConcurrencyType

• Separate contexts for each thread•MOCs only used on thread or queue that created them•Default, legacy option

Page 15: Session 214 Core Data Best Practices

Confinement with Thread or Queue

• Thread or serialized queue as single control flow• Serialized dispatch queues•NSOperationQueue with maximum concurrency one

Page 16: Session 214 Core Data Best Practices

Thread Confinement

• Safe and efficient for transactions• Easy to understand• But harder to manage

Page 17: Session 214 Core Data Best Practices

Thread Confinement Issues

• Tracking which context goes with which thread• Potentially keeping extra threads around•Main thread behaviors inferred•User events are runloop driven

Page 18: Session 214 Core Data Best Practices

NSPrivateQueueConcurrencyType

•MOC maintains its own serialized queue• Can only be used on its own private queue•Use -performBlock: and -performBlockAndWait: from other threads•Within block use APIs normally

Page 19: Session 214 Core Data Best Practices

Queue Is Private

•Do not use dispatch_get_current_queue• Callback to your own queue with dispatch_sync• Capture references in your blocks

Page 20: Session 214 Core Data Best Practices

Private Queue Advantages

•MOC responsible for routing blocks to correct queue•Other threads just call -performBlock:• Can be created from any other thread• Idle queues more efficient than extra threads

Page 21: Session 214 Core Data Best Practices

NSMainQueueConcurrencyType

• Similar to private queue•Queue is always the main queue•Non-main threads must use -performBlock:•User events driven by main runloop

Page 22: Session 214 Core Data Best Practices

NSMainQueueConcurrencyType

•UI and controllers on main thread can use•Great for receiving results from background•Always uses main thread behaviors

Page 23: Session 214 Core Data Best Practices

Main Queue

MOC

Block

UI Controllers

Another Thread

Using Main Queue MOCs

Block

Using MOC

Page 24: Session 214 Core Data Best Practices

What’s a User Event?

•Automatic as application main event loop• Provides

■ Change coalescing■ Delete propagation■ Undo■ NSNotifications

• Time in between calls to -processPendingChanges

Page 25: Session 214 Core Data Best Practices

For All Concurrency Types

•Managed objects owned by their context•ObjectIDs are safe, immutable value objects• Retain, release are always thread safe on Core Data objects

Page 26: Session 214 Core Data Best Practices

Good Times to Pass Updates Around

•NSManagedObjectContextObjectsDidChangeNotification•NSManagedObjectContextDidSaveNotification

Page 27: Session 214 Core Data Best Practices

Refreshing Other MOCs After Save

mergeChangesFromContextDidSaveNotification:

• You are only responsible for thread safety of receiver• Core Data handles issues with notification parameter

Page 28: Session 214 Core Data Best Practices

Useful NSManagedObject Methods

changedValuesForCurrentEventchangedValuescommittedValuesForKeys

Page 29: Session 214 Core Data Best Practices

Block-Based APIs

Page 30: Session 214 Core Data Best Practices

Challenges

• Passing work to other threads•Demarcating cohesive changes• Integrating with platform concurrency APIs

Page 31: Session 214 Core Data Best Practices

-performBlock:

•Asynchronous•A “user event”• Convenient autorelease pool•No support for reentrancy• Illegal to throw an exception out of your block

Page 32: Session 214 Core Data Best Practices

MOC’s Queue

Recursive performBlock

Block 1 Block 2 Block 3

Page 33: Session 214 Core Data Best Practices

-performBlockAndWait:

• Synchronous•Not an event•No autorelease pool• Supports reentrancy• Illegal to throw an exception out of your block

Page 34: Session 214 Core Data Best Practices

Recursive performBlockAndWait

MOC’s Queue

Block 1

Block 2

Block3

Page 35: Session 214 Core Data Best Practices

NSManagedObjectContext Block APIs

• Fast• Lightweight • Serialized• Changes scoped by block

Page 36: Session 214 Core Data Best Practices

Working with Data Between Blocks

•ObjectIDs often useful■ Rematerialize into MO with objectWithID:■ objectWithID will reuse cached data

•Also, okay to retain MOs but not look or use outside block•Use __block variables• Remember NSError are autoreleased

Page 37: Session 214 Core Data Best Practices

Fetching from a Private Queue MOC

__block NSArray* oids = nil;![context performAndWait:^(NSManagedObjectContext* moc) { NSError *error = nil; NSArray* results = [moc executeFetchRequest:fr error:&error]; if (results != nil) { oids = [results valueForKey:@”objectID”]; } else {

// handle error }}];!NSLog(@"retrieved %d items", (int)[oids count]);

Page 38: Session 214 Core Data Best Practices

Coordinating Using Blocks

__block dispatch_queue_t yourQueue;

[context perform:^(NSManagedObjectContext* moc) { // workdispatch_sync(yourQueue, ^(){ // callback work });

}];!

Page 39: Session 214 Core Data Best Practices

Coordinating Using Semaphores

__block dispatch_semaphore_t waiter = dispatch_semaphore_create(0);

[context perform:^(NSManagedObjectContext* moc) { // workdispatch_semaphore_signal(waiter);

}];!dispatch_semaphore_wait(waiter, yourtimeout);

Page 40: Session 214 Core Data Best Practices

Interfacing with libdispatch

• Create a dispatch group• Call dispatch_group_enter•Worker block call dispatch_group_leave•Use dispatch_group_wait and dispatch_group_notify normally

Page 41: Session 214 Core Data Best Practices

Coordinating Using Groups

__block dispatch_group_t group = dispatch_group_create();

dispatch_group_enter(group);

[context perform:^(NSManagedObjectContext* moc) { // workdispatch_group_leave(group);

}];!dispatch_group_wait(group, yourtimeout);

Page 42: Session 214 Core Data Best Practices

Nested Contexts

Page 43: Session 214 Core Data Best Practices

Challenges

• Sharing unsaved changes•Asynchronous saving

Page 44: Session 214 Core Data Best Practices

Nested Contexts

• Parent context acts like a persistent store for the child• Children see state as it is in the parent• Children inherit unsaved changes from parent• Children marshal their saves in memory to the parent

Page 45: Session 214 Core Data Best Practices

Parent

Child

Store

MOC 2

MOC 1

Nested Contexts

Page 46: Session 214 Core Data Best Practices

Why Use Nested Contexts?

• Sharing unsaved changes between MOCs•Asynchronous saves• Inheriting changes in a detail inspector

Page 47: Session 214 Core Data Best Practices

Sharing Unsaved Changes

• Push to parent context with save• Pull in peer contexts

■ Fetching■ Merging■ Refreshing

Page 48: Session 214 Core Data Best Practices

Asynchronous Save

• Save child•Asynchronously ask parent to save• Changes not written to disk until root parent saves

Page 49: Session 214 Core Data Best Practices

Asynchronous Save

NSManagedObjectContext *child, *parent;parent = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];[child setParentContext:parent];// ...[child save:&error]; [parent performBlock:^{

[parent save:&parentError];}];

Page 50: Session 214 Core Data Best Practices

Inheriting Changes in Detail Inspector

• Create a child context• Save pushes changes into parent• Fetch incorporates unsaved changes in parent• Toss child context to cancel detail changes

Page 51: Session 214 Core Data Best Practices

Things to Remember

• Saving only pushes changes up one level• Fetching pulls data through all levels• -objectWithID: pulls fewest levels necessary• Parent contexts must adopt a queue type

Page 52: Session 214 Core Data Best Practices

Child MOCs Depend on Their Parents

• Parent context must not block upon children• Children MOCs can performAndWait on parent• Parents cannot performAndWait on children• Requests flow up the tree of MOCs• Results flow down from the parent

Page 53: Session 214 Core Data Best Practices

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

Session 214

Performance

Melissa TurnerSr Core Data Engineer

Page 54: Session 214 Core Data Best Practices

Recognizing problems

• Environment•Application should do•Application does do

Page 55: Session 214 Core Data Best Practices

Recognizing problems

• Environment•Application should do•Application does do

Page 56: Session 214 Core Data Best Practices

Environment

• Test on your minimal configuration•Design for environment

■ Network

• Sufficient vs optimal

Page 57: Session 214 Core Data Best Practices

ShouldUser driven workflow

Page 58: Session 214 Core Data Best Practices

ShouldUser driven workflow

File Access

Page 59: Session 214 Core Data Best Practices

ShouldUser driven workflow

File Access

Network Access

Page 60: Session 214 Core Data Best Practices

ShouldUser driven workflow

File Access

Network Access

Background processing

Page 61: Session 214 Core Data Best Practices

ShouldAutomatic processing

Page 62: Session 214 Core Data Best Practices

ShouldAutomatic processing

Updates

Page 63: Session 214 Core Data Best Practices

ShouldAutomatic processing

Updates Notifications

Page 64: Session 214 Core Data Best Practices

ShouldAutomatic processing

Updates Notifications BackgroundProcessing

Page 65: Session 214 Core Data Best Practices

Measure, measure, measureDoes

Time Profiler

Page 66: Session 214 Core Data Best Practices

Measure, measure, measureDoes

Core Data Template

Page 67: Session 214 Core Data Best Practices

Measure, measure, measureDoes

-com.apple.CoreData.SQLDebug 1 (or 3)

Page 68: Session 214 Core Data Best Practices

Measure twice.Cut once.

Page 69: Session 214 Core Data Best Practices

The Target—Table Views

• Easy to visualize■ Too much data■ Too little data■ Badly formed data

• Lessons are generally applicable•Disclaimer

Page 70: Session 214 Core Data Best Practices

In the Beginning…

Page 71: Session 214 Core Data Best Practices

Schema Version 1

Page 72: Session 214 Core Data Best Practices

Schema Version 1

Rome vacation

Page 73: Session 214 Core Data Best Practices

Schema Version 1

Colosseum Architecture

Page 74: Session 214 Core Data Best Practices

Schema Version 1

Rome vacationColosseum Architecture

Page 75: Session 214 Core Data Best Practices

Schema Version 1

Rome vacationColosseum Architecture

Page 76: Session 214 Core Data Best Practices

Schema Version 1

Photo

AttributeslabelphotoBlobtagstimestamp

Relationships

Page 77: Session 214 Core Data Best Practices

DemoNot fast, ergo furious

Page 78: Session 214 Core Data Best Practices

Ground zero for data optimizationNSFetchRequest

• Batching• Fetch limits and offsets• Predicates•Grouping•Aggregates

Page 79: Session 214 Core Data Best Practices

Ground zero for data optimizationNSFetchRequest

• Batching• Fetch limits and offsets• Predicates•Grouping•Aggregates

<x-coredata://1>

<x-coredata://2>

<x-coredata://3>

<x-coredata://4>

<x-coredata://5>

<x-coredata://6>

<x-coredata://7>

Page 80: Session 214 Core Data Best Practices

Ground zero for data optimizationNSFetchRequest

• Batching• Fetch limits and offsets• Predicates•Grouping•Aggregates

<x-coredata://1>

<x-coredata://2>

<x-coredata://3>

<x-coredata://4>

<x-coredata://5>

<x-coredata://6>

<x-coredata://7>

Rome 3 2012/01/02

Rome 12012/02/01

Rome 22012/02/01

Page 81: Session 214 Core Data Best Practices

Ground zero for data optimizationNSFetchRequest

• Batching• Fetch limits and offsets• Predicates•Grouping•Aggregates

<x-coredata://1>

<x-coredata://2>

<x-coredata://3>

<x-coredata://4>

<x-coredata://5>

<x-coredata://6>

<x-coredata://7>

Rome 3 2012/01/02

Rome 12012/02/01

Rome 22012/02/01

Rome 6 2012/01/03

Rome 52012/02/03

Rome 42012/02/02

Page 82: Session 214 Core Data Best Practices

Ground zero for data optimizationNSFetchRequest

• Batching• Fetch limits and offsets• Predicates•Grouping•Aggregates

Page 83: Session 214 Core Data Best Practices

Ground zero for data optimizationNSFetchRequest

• Batching• Fetch limits and offsets• Predicates•Grouping•Aggregates

Rome 3 2012/01/02

Rome 12012/02/01

Rome 22012/02/01

Page 84: Session 214 Core Data Best Practices

Ground zero for data optimizationNSFetchRequest

• Batching• Fetch limits and offsets• Predicates•Grouping•Aggregates

Rome 6 2012/01/03

Rome 52012/02/03

Rome 42012/02/02

Page 85: Session 214 Core Data Best Practices

Ground zero for data optimizationNSFetchRequest

• Batching• Fetch limits and offsets• Predicates•Grouping•Aggregates

Page 86: Session 214 Core Data Best Practices

Ground zero for data optimizationNSFetchRequest

• Batching• Fetch limits and offsets• Predicates•Grouping•Aggregates

Rome 42012/02/02

Rome 32012/02/02

Page 87: Session 214 Core Data Best Practices

There is no one true schemaSchema Design and Optimization

Page 88: Session 214 Core Data Best Practices

Build a solid foundationDesigning Your Schema

•Application concept will drive UI•UI will drive schema•No one true schema

Page 89: Session 214 Core Data Best Practices

Eliminate data duplicationNormalization

• Reduce possibility of skew•Minimize storage space•Minimize memory usage• Faster searching

Page 90: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Tags

Family, Vacation

Family, Work

Work

Page 91: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Tags

Family, Vacation

Family, Work

Work

Page 92: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Tags

Family, Vacation

Family, Work

Work

Page 93: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Tags

Family, Vacation

Family, Work

Work

Page 94: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Label

Family

Vacation

Work

Page 95: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Label

Family

Vacation

Work

Page 96: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Page 97: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Page 98: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Page 99: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp Data

Rome 2012/02/01 <abcde000 ...>

Hawaii 2012/02/01 <012409e0 ...>

Beijing 2012/02/12 <deadbeef ...>

Page 100: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp

Rome 2012/02/01

Hawaii 2012/02/01

Beijing 2012/02/12

Data

<abcde000 ...>

<012409e0 ...>

<deadbeef ...>

Page 101: Session 214 Core Data Best Practices

NormalizationMinimize storage space

Label Timestamp

Rome 2012/02/01

Hawaii 2012/02/01

Beijing 2012/02/12

Data

<abcde000 ...>

<012409e0 ...>

<deadbeef ...>

Page 102: Session 214 Core Data Best Practices

External Data References

•Move data out of store into co-located file• Best stored on dedicated objects• Refresh object after initial save

Page 103: Session 214 Core Data Best Practices

Schema Version 2Breakdown

Photo

AttributeslabelphotoBlobtagstimestamp

Relationships

Page 104: Session 214 Core Data Best Practices

Schema Version 2Breakdown

Attributeslabel

Relationshipsphotos

Tag

Attributesbytes

Relationshipsphoto

PhotoBlob Photo

AttributeslabeltimeStamp

RelationshipsphotoBlobtags

Page 105: Session 214 Core Data Best Practices

DemoGetting closer

Page 106: Session 214 Core Data Best Practices

Reduce number of joinsDenormalization

•Minimize repeated transforms•Minimize relationship fault firing

■ Store relationship meta-information on source■ Existence■ Count■ Aggregate values

Page 107: Session 214 Core Data Best Practices

NormalizationMinimize relationship fault firing

Label Timestamp

Rome 2012/02/01

Hawaii 2012/02/01

Beijing 2012/02/12

Label

Family

Vacation

Work

Page 108: Session 214 Core Data Best Practices

NormalizationMinimize relationship fault firing

Label Timestamp TagCount

Rome 2012/02/01 2

Hawaii 2012/02/01 2

Beijing 2012/02/12 1

Label

Family

Vacation

Work

Page 109: Session 214 Core Data Best Practices

DenormalizationMinimize repeated transforms

Label Timestamp TagCount

Rome 2012/02/01 2

Hawaii 2012/02/01 2

Beijing 2012/02/12 1

Data

<abcde000 ...>

<012409e0 ...>

<deadbeef ...>

Page 110: Session 214 Core Data Best Practices

DenormalizationMinimize repeated transforms

Label Timestamp TagCount

Rome 2012/02/01 2

Hawaii 2012/02/01 2

Beijing 2012/02/12 1

Data

<abcde000 ...>

<012409e0 ...>

<deadbeef ...>

Thumbnail

<12480f0f ...>

<cafebabe ...>

<66666600 ...>

Page 111: Session 214 Core Data Best Practices

CanonicalizationMinimize relationship fault firing

Label Timestamp TagCount

Rome 2012/02/01 2

Hawaii 2012/02/01 2

Beijing 2012/02/12 1

Data

<abcde000 ...>

<012409e0 ...>

<deadbeef ...>

Thumbnail

<12480f0f ...>

<cafebabe ...>

<66666600 ...>

Page 112: Session 214 Core Data Best Practices

CanonicalizationMinimize relationship fault firing

Label Timestamp TagCount

Rome 2012/02/01 2

Hawaii 2012/02/01 2

Beijing 2012/02/12 1

Data

<abcde000 ...>

<012409e0 ...>

<deadbeef ...>

Thumbnail

<12480f0f ...>

<cafebabe ...>

<66666600 ...>

Page 113: Session 214 Core Data Best Practices

Search Optimization

Page 114: Session 214 Core Data Best Practices

Before You Start the Fetch

• Initial view empty• Search as you type vs on completion

Page 115: Session 214 Core Data Best Practices

Strategies for making searches fasterSearching

• String canonicalization• Fetch tokens• Canonical objects

Page 116: Session 214 Core Data Best Practices

CanonicalizationMinimize CPU cycles

Label

Family

Vacation

Work

Page 117: Session 214 Core Data Best Practices

CanonicalizationMinimize CPU cycles

Label SearchString

Family family

Vacation vacation

Work work

Page 118: Session 214 Core Data Best Practices

Regex is not your user’s friendPlaying with Strings

• Case and diacritic insensitivity• beginswith vs contains•Avoidance of wildcards

Page 119: Session 214 Core Data Best Practices

Predicate Optimization

label LIKE[cd] “Red*”

Page 120: Session 214 Core Data Best Practices

Predicate Optimization

searchString BEGINSWITH[n] “red”

Page 121: Session 214 Core Data Best Practices

Predicate Optimization

label MATCHES[cd] “.*Red.*”

Page 122: Session 214 Core Data Best Practices

Predicate Optimization

searchString CONTAINS “red”

Page 123: Session 214 Core Data Best Practices

Predicate Optimization

label MATCHES[cd] “.*Red.blue.*”

Page 124: Session 214 Core Data Best Practices

Predicate Optimization

searchString MATCHES[n] “.*red.blue.*”

Page 125: Session 214 Core Data Best Practices

Order mattersOptimizing Your Predicate

•No query optimizer in SQLite• Eliminate largest groups first•Group size vs comparison speed• Put heaviest operations last

Page 126: Session 214 Core Data Best Practices

Predicate Optimization

searchString CONTAINS “red” OR timestamp BETWEEN (X, Y)

Page 127: Session 214 Core Data Best Practices

Predicate Optimization

timestamp BETWEEN (X, Y) OR searchString CONTAINS “red”

Page 128: Session 214 Core Data Best Practices

ANY tag.searchString == “rome” AND label == “rome”

Predicate Optimization

Page 129: Session 214 Core Data Best Practices

ANY tag IN FETCH(%@, %@, NO) AND label == “red”

Predicate Optimization

Page 130: Session 214 Core Data Best Practices

Query Optimization

Label Timestamp TagCount Thumbnail

Rome 2012/02/01 2 <12480f0f ...>

Hawaii 2012/02/01 2 <cafebabe ...>

Beijing 2012/02/12 1 <66666600 ...>

Rome 2011/01/01 0 <87654320 ...>

San Francisco 2011/12/12 2 <42424240 ...>

Page 131: Session 214 Core Data Best Practices

Query Optimization

Label Timestamp TagCount Thumbnail

Rome 2012/02/01 2 <12480f0f ...>

Hawaii 2012/02/01 2 <cafebabe ...>

Beijing 2012/02/12 1 <66666600 ...>

Rome 2011/01/01 0 <87654320 ...>

San Francisco 2011/12/12 2 <42424240 ...>

Page 132: Session 214 Core Data Best Practices

Query Optimization

Label Timestamp TagCount Thumbnail

Rome 2012/02/01 2 <12480f0f ...>

Hawaii 2012/02/01 2 <cafebabe ...>

Beijing 2012/02/12 1 <66666600 ...>

Rome 2011/01/01 0 <87654320 ...>

San Francisco 2011/12/12 2 <42424240 ...>

Page 133: Session 214 Core Data Best Practices

Query Optimization

Label Timestamp TagCount Thumbnail

Rome 2012/02/01 2 <12480f0f ...>

Hawaii 2012/02/01 2 <cafebabe ...>

Beijing 2012/02/12 1 <66666600 ...>

Page 134: Session 214 Core Data Best Practices

Query Optimization

Label Timestamp TagCount Thumbnail

Rome 2012/02/01 2 <12480f0f ...>

Hawaii 2012/02/01 2 <cafebabe ...>

Beijing 2012/02/12 1 <66666600 ...>

Page 135: Session 214 Core Data Best Practices

Schema Version 3

Attributeslabel

Relationshipsphotos

Tag

Attributesbytes

Relationshipsphoto

PhotoBlob Photo

AttributeslabeltimeStamp

RelationshipsphotoBlobtags

Page 136: Session 214 Core Data Best Practices

Schema Version 3

Photo

AttributeslabelthumbnailtimeStamp

RelationshipsphotoBlobtags

AttributescanonicalLabellabel

Relationshipsphotos

Tag

Attributesbytes

Relationshipsphoto

PhotoBlob

Page 137: Session 214 Core Data Best Practices

DemoMuch better

Page 138: Session 214 Core Data Best Practices

Get rid of no-longer-interesting dataCleaning Up

•Autorelease pools• -[NSManagedObjectContext refreshObject: mo mergeChanges: [mo hasChanges]]• -[NSManagedObjectContext reset]

Page 139: Session 214 Core Data Best Practices

Today’s Roadmap

• Concurrency•Nested Contexts• Performance• Schema Design• Search Optimization

Page 140: Session 214 Core Data Best Practices

http://bugreport.apple.com

•We don’t know unless you tell us• Bugs fixed faster with

■ Steps to reproduce■ Sample project

•Also use for ■ Feature requests■ Enhancement requests■ Performance issues■ Documentation requests

Page 141: Session 214 Core Data Best Practices

More Information

Michael JurewitzTechnology [email protected]

Cocoa [email protected]

Core Data DocumentationProgramming Guides, Examples, Tutorialshttp://developer.apple.com/

Apple Developer Forumshttp://devforums.apple.com

Page 142: Session 214 Core Data Best Practices

Related Sessions

Using iCloud with Core Data MissionWednesday 4:30PM

Page 143: Session 214 Core Data Best Practices

Labs

Core Data Lab Essentials Lab AWednesday 2:00PM

Core Data Lab Developer Tools Lab AThursday 9:00AM

Core Data Lab Essentials Lab BFriday 9:00AM

Page 144: Session 214 Core Data Best Practices
Page 145: Session 214 Core Data Best Practices

The last 3 slides after the logo are intentionally left blank for all presentations.

Page 146: Session 214 Core Data Best Practices

The last 3 slides after the logo are intentionally left blank for all presentations.

Page 147: Session 214 Core Data Best Practices

The last 3 slides after the logo are intentionally left blank for all presentations.