What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New...
Transcript of What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New...
![Page 1: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/1.jpg)
© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
#WWDC15
What’s New in Core Data
Rishi Verma Core Data EngineerScott Perry Core Data Engineer
App Frameworks
Session 220
![Page 2: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/2.jpg)
What Is Core Data?To persist or not to persist
Rishi Verma Core Data Engineer
![Page 3: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/3.jpg)
Object Graph ManagementManage my graph with Core Data
Bridge your data simply into a Cocoa Model LayerPersist data in the back end of your choice
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
![Page 4: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/4.jpg)
Automatic Graph ManagementRelationships can be complicated…
Automatic relationship maintenance
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
![Page 5: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/5.jpg)
NSFetchRequestFinding a needle in a haystack
Find the data you need
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
![Page 6: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/6.jpg)
NSFetchRequestFinding a needle in a haystack
Find the data you need
Apples
Apple Apple Pie
Snack Dessert
Apple Butter
Recipes
Sugar
Apple Bread
![Page 7: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/7.jpg)
NSFetchRequestFinding a needle in a haystack
Find the data you needBatching
Apples Sugar
Apple Apple Bread
Snack
Apple Butter Apple Pie
Dessert
Recipes
![Page 8: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/8.jpg)
NSFetchRequestFinding a needle in a haystack
Find the data you needBatching Relationship prefetching
Apple Pie Apple Bread
Dessert
Recipes
Apples Sugar
AppleApple Butter
Snack
![Page 9: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/9.jpg)
NSFetchRequestFinding a needle in a haystack
Find the data you needBatching Relationship prefetching
Apple Pie Apple Bread
Dessert
Recipes
Apples Sugar
AppleApple Butter
Snack
![Page 10: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/10.jpg)
NSFetchRequestFinding a needle in a haystack
Find the data you needBatching Relationship prefetching
Then tie this with your UI and…
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
![Page 11: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/11.jpg)
View and Controller SupportMy UI brings all the updates to the users
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
![Page 12: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/12.jpg)
My UI brings all the updates to the users
Apple Apple Pie Apple Bread
Snack Dessert
Recipes
View and Controller Support
![Page 13: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/13.jpg)
My UI brings all the updates to the users
Apple Apple Pie Apple Bread
Snack Dessert
Recipes
View and Controller Support
![Page 14: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/14.jpg)
My UI brings all the updates to the usersView and Controller Support
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
Banana Bread
![Page 15: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/15.jpg)
My UI brings all the updates to the usersView and Controller Support
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
Banana Bread
![Page 16: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/16.jpg)
Multi-Writer Conflict HandlingOn your mark, set that merge policy, and done
CoreData versions all objects Several types of merge policies available • Defaults to error• Persistent store vs. in-memory
![Page 17: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/17.jpg)
Memory EfficienciesAPIs with benefits
Excellent memory scalabilityAggressive lazy loading
Memory
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
Apple Apple Bread
Apple Butter
Apples
Apple Pie
Snack Dessert
Recipes
![Page 18: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/18.jpg)
Memory EfficienciesAPIs with benefits
Excellent memory scalabilityAggressive lazy loading
Memory
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
Apple Apple Bread
Apple Butter
Apples Apple Pie SnackDessert Recipes
![Page 19: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/19.jpg)
Memory EfficienciesAPIs with benefits
Excellent memory scalabilityAggressive lazy loading
Memory
Apples Sugar
Apple Apple Pie Apple Bread
Snack Dessert
Apple Butter
Recipes
AppleApple Bread
Apple Butter
![Page 20: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/20.jpg)
Smaller FootprintLess is more
![Page 21: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/21.jpg)
![Page 22: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/22.jpg)
50%–70%Less code
![Page 23: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/23.jpg)
![Page 24: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/24.jpg)
400,000
![Page 25: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/25.jpg)
API Enhancements
![Page 26: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/26.jpg)
hasPersistentChangedValuesNSManagedObject
var hasPersistentChangedValues: Bool { get }
No false positives setting a value to itselfSkips transient properties
![Page 27: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/27.jpg)
objectIDsForRelationshipNamedNSManagedObject
func objectIDsForRelationshipNamed(key: String) -> [NSManagedObjectID]
Reads cache or fetches the objectIDsDoesn’t materialize entire relationshipUseful working with large, many-to-many relationships
![Page 28: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/28.jpg)
objectIDsForRelationshipNamedCode example
![Page 29: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/29.jpg)
objectIDsForRelationshipNamedCode example
let relations = person.objectIDsForRelationshipNamed("family")
![Page 30: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/30.jpg)
objectIDsForRelationshipNamedCode example
let relations = person.objectIDsForRelationshipNamed("family")
let fetchFamily = NSFetchRequest(entityName: “Person") fetchFamily.fetchBatchSize = 100 fetchFamily.predicate = NSPredicate(format: "self IN %@", relations)
![Page 31: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/31.jpg)
objectIDsForRelationshipNamedCode example
let relations = person.objectIDsForRelationshipNamed("family")
let fetchFamily = NSFetchRequest(entityName: “Person") fetchFamily.fetchBatchSize = 100 fetchFamily.predicate = NSPredicate(format: "self IN %@", relations)
let batchedRelations = managedObjectContext.executeFetchRequest(fetchFamily)
![Page 32: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/32.jpg)
objectIDsForRelationshipNamedCode example
let relations = person.objectIDsForRelationshipNamed("family")
let fetchFamily = NSFetchRequest(entityName: “Person") fetchFamily.fetchBatchSize = 100 fetchFamily.predicate = NSPredicate(format: "self IN %@", relations)
let batchedRelations = managedObjectContext.executeFetchRequest(fetchFamily)
for relative in batchedRelations { // work with relations 100 rows at a time
}
![Page 33: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/33.jpg)
refreshAllObjectsNSManagedObjectContext
func refreshAllObjects()
Affects all registered objects in a contextPreserves unsaved changesManaged Object references remain validUseful for breaking retain cycles
![Page 34: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/34.jpg)
mergeChangesFromRemoteContextSaveNSManagedObjectContext
class func mergeChangesFromRemoteContextSave(changeNotificationData: [NSObject : AnyObject], intoContexts contexts: [NSManagedObjectContext])
Better for changes from different coordinatorsFetches latest row dataHandles ordering with nested contexts
![Page 35: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/35.jpg)
No Love for ExceptionsThis is not the data you are looking for
Why is Core Data unable to fulfill a fault?Managed objects are implicit futures• Cocoa place holders for a row of data• Often lazily loaded• Part of a larger graph
Data deleted out from underneath this reference
![Page 36: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/36.jpg)
shouldDeleteInaccessibleFaultsNSManagedObjectContext
var shouldDeleteInaccessibleFaults: Bool
• Defaults to YES• Does not effect APIs with error parameters
Bad faults marked deletedMissing data treated as NULL/nil/0
![Page 37: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/37.jpg)
NSPersistentStoreCoordinator APIIt’s my file and I’ll do what I want to
Truncating and copying databasesDon’t bypass the API layers• NSFileManager and POSIX are bad for databases• Will corrupt your files if open connections exist
Deleting a file with open locks ends badly…very badly
![Page 38: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/38.jpg)
destroyPersistentStoreAtURLNSPersistentStoreCoordinator
func destroyPersistentStoreAtURL(url: NSURL, withType storeType: String, options: [NSObject : AnyObject]?) throws
Honors locking protocolsHandles details reconfiguring emptied files• Journal mode, page size, etc.• Need to pass same options as addToPersistentStore• Accidentally switching journal modes can deadlock
![Page 39: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/39.jpg)
replacePersistentStoreAtURLNSPersistentStoreCoordinator
func replacePersistentStoreAtURL(destinationURL: NSURL, destinationOptions: [NSObject : AnyObject]?, withPersistentStoreFromURL sourceURL: NSURL, sourceOptions: [NSObject : AnyObject]?, storeType: String) throws
Same pattern as destroyPersistentStoreAtURLIf destination doesn’t exist, this does a copy
![Page 40: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/40.jpg)
Unique ConstraintsI got 99 problems and they are all duplicates…
![Page 41: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/41.jpg)
Find or Create PatternUnique constraints
managedObjectContext.performBlock { let createRequest = NSFetchRequest(entityName: "Recipe") createRequest.resultType = ManagedObjectIDResultType let predicate = NSPredicate(format: "source = %@ AND externalID = %@", source,externalID) let results = self.managedObjectContext.executeFetchRequest(createRequest) if (results.count) { //update it! } else { //create it! } }
![Page 42: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/42.jpg)
One of a KindUnique constraints
Unique attributes across all instances of an entity• Email addresses • Part numbers• UPC• ISBN• Unique key/value pairs
![Page 43: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/43.jpg)
Best PracticesUnique constraints
Best for values unmodified after object creationSub-entities may extend constraints• Parent (UUID)• Sub-entity (UUID, EMAIL)
Recovery uses merge policies
![Page 44: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/44.jpg)
DemoHow to utilize unique constraints
![Page 45: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/45.jpg)
Deleting Multiple ObjectsTake one down, pass it around…
Scott Perry Code Generator
![Page 46: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/46.jpg)
Object DeletionThe problem
Today, deleting objects requires Application Memory
PersistentConfiguration
Storage
![Page 47: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/47.jpg)
Object DeletionThe problem
Today, deleting objects requires• Fetching some objects
Application Memory
PersistentConfiguration
Storage
![Page 48: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/48.jpg)
Object DeletionThe problem
Today, deleting objects requires• Fetching some objects• Marking each object for deletion
Application Memory
PersistentConfiguration
Storage
![Page 49: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/49.jpg)
Object DeletionThe problem
Today, deleting objects requires• Fetching some objects• Marking each object for deletion• Saving the changes
Application Memory
PersistentConfiguration
Storage
![Page 50: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/50.jpg)
Object DeletionThe problem
Today, deleting objects requires• Fetching some objects• Marking each object for deletion• Saving the changes• Repeat
Application Memory
PersistentConfiguration
Storage
![Page 51: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/51.jpg)
Object DeletionThe problem
You shouldn’t have to load objects into memory to delete them
![Page 52: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/52.jpg)
NSBatchDeleteRequestThe solution
Very similar to NSBatchUpdateRequest• Acts directly on the Persistent Store
![Page 53: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/53.jpg)
NSBatchDeleteRequestThe solution
Very similar to NSBatchUpdateRequest• Acts directly on the Persistent Store
Instances of NSBatchDeleteRequest wrap an instance of NSFetchRequest• One entity• One or more stores• Supports predicates as well as sort descriptors and offset/limit
![Page 54: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/54.jpg)
NSBatchDeleteResultThe solution
Success/failureCount of objects deletedObject IDs of objects deleted
![Page 55: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/55.jpg)
Batch DeletionsLimitations
Changes are not reflected in the contextNot all validation rules are enforcedNo object notifications
![Page 56: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/56.jpg)
DemoNSBatchDeleteRequest
![Page 57: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/57.jpg)
Model Versioning
![Page 58: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/58.jpg)
Models Change
Recipe
instructions
name
overview
prepTime
thumbnailImage
image
ingredients
type
![Page 59: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/59.jpg)
Models ChangeRecipe
instructions
name
overview
prepTime
thumbnailImage
image
ingredients
type
![Page 60: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/60.jpg)
Models ChangeRecipe
instructions
name
overview
prepTime
thumbnailImage
image
ingredients
type
source
externalID
![Page 61: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/61.jpg)
Models Change…But migrations stay the same
Error Domain=NSCocoaErrorDomain Code=134130 "Persistent store migration failed, missing source managed object model." UserInfo=0x1054a2380 { URL=file:///private/var/mobile/Containers/Data/Application/6CD803A7-91EC… metadata={ NSPersistenceFrameworkVersion = 619; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( "" ); NSStoreType = SQLite; NSStoreUUID = "EF65B546-1D30-48A4-9090-E274F4DF7822"; "_NSAutoVacuumLevel" = 2; NSStoreModelVersionHashes = { Recipe = <81b7e3b1 450cf990 6f1c8f36 89786a0b f61715cb afd9016b … … }; }, reason=Can't find model for source store }
![Page 62: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/62.jpg)
Models Change…But migrations stay the same
Error Domain=NSCocoaErrorDomain Code=134130 "Persistent store migration failed, missing source managed object model." UserInfo=0x1054a2380 { URL=file:///private/var/mobile/Containers/Data/Application/6CD803A7-91EC… metadata={ NSPersistenceFrameworkVersion = 619; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( "" ); NSStoreType = SQLite; NSStoreUUID = "EF65B546-1D30-48A4-9090-E274F4DF7822"; "_NSAutoVacuumLevel" = 2; NSStoreModelVersionHashes = { Recipe = <81b7e3b1 450cf990 6f1c8f36 89786a0b f61715cb afd9016b … … }; }, reason=Can't find model for source store }
![Page 63: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/63.jpg)
Iterating models is cumbersomeForgetting to deploy model versions is dangerous
Models ChangeThe problem
![Page 64: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/64.jpg)
Models ChangeThe problem
Iterating models is cumbersomeForgetting to deploy model versions is dangerousAutomatic lightweight migrations should “Just Work™”
![Page 65: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/65.jpg)
Model CachingThe solution
NSManagedObjectModel copied to storeAutomatically updates existing storesLightweight migrations fetch the model from the store
![Page 66: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/66.jpg)
Only SQLite storesCached model is not available to explicit migrations
Model CachingLimitations
![Page 67: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/67.jpg)
API Modernization
![Page 68: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/68.jpg)
Generics and NullabilityBetter living through more explicit types
nonnull (default), nullable, and null_resettable__kindof allows for easier casting
![Page 69: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/69.jpg)
id
Generics and NullabilityBetter living through more explicit types
nonnull (default), nullable, and null_resettable__kindof allows for easier casting
Recipe Ingredient
NSStringNSManagedObject
NSObject
![Page 70: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/70.jpg)
Generics and NullabilityBetter living through more explicit types
nonnull (default), nullable, and null_resettable__kindof allows for easier casting
__kindof NSManagedObject *
Recipe Ingredient
NSStringNSManagedObject
NSObject
![Page 71: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/71.jpg)
Generics and NullabilityBetter living through more explicit types
nonnull (default), nullable, and null_resettable__kindof allows for easier castingGenerated subclasses use generics for to-many relationships
![Page 72: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/72.jpg)
Generated Subclasses
Subclass.h
Subclass.mSubclass.swift
![Page 73: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/73.jpg)
Generated Subclasses
Subclass.h
Subclass+NSManagedProperties.h
Subclass.m
Subclass.swift
Subclass+NSManagedProperties.swift
![Page 74: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/74.jpg)
Generated Subclasses
Subclass+NSManagedProperties.hSubclass+NSManagedProperties.swift
![Page 75: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/75.jpg)
import Foundation import CoreData
extension Recipe {
@NSManaged var thumbnailImage: NSObject? @NSManaged var source: String? @NSManaged var instructions: String? @NSManaged var prepTime: String? @NSManaged var overview: String? @NSManaged var externalID: String? @NSManaged var name: String? @NSManaged var ingredients: NSSet? @NSManaged var image: NSManagedObject? @NSManaged var type: NSManagedObject?
}
#import "Recipe.h"
NS_ASSUME_NONNULL_BEGIN
@interface Recipe (CoreDataProperties)
@property (nullable, nonatomic, retain) id thumbnailImage; @property (nullable, nonatomic, retain) NSString *source; @property (nullable, nonatomic, retain) NSString *instructions; @property (nullable, nonatomic, retain) NSString *prepTime; @property (nullable, nonatomic, retain) NSString *overview; @property (nullable, nonatomic, retain) NSString *externalID; @property (nullable, nonatomic, retain) NSString *name; @property (nullable, nonatomic, retain) NSSet<Ingredient *> *ingredients; @property (nullable, nonatomic, retain) NSManagedObject *image; @property (nullable, nonatomic, retain) NSManagedObject *type;
@end
@interface Recipe (CoreDataGeneratedAccessors)
- (void)addIngredientsObject:(Ingredient *)value; - (void)removeIngredientsObject:(Ingredient *)value; - (void)addIngredients:(NSSet<Ingredient *> *)values; - (void)removeIngredients:(NSSet<Ingredient *> *)values;
@end
NS_ASSUME_NONNULL_END
Generated SubclassesSubclass+NSManagedProperties.h Subclass+NSManagedProperties.swift
![Page 76: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/76.jpg)
ConcurrencyConfinement is dead, long live queues
![Page 77: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/77.jpg)
ConcurrencyConfinement is dead, long live queues
ConfinementConcurrencyType is deprecated
![Page 78: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/78.jpg)
ConcurrencyConfinement is dead, long live queues
ConfinementConcurrencyType is deprecatedinit() has been deprecated
![Page 79: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/79.jpg)
ConcurrencyConfinement is dead, long live queues
ConfinementConcurrencyType is deprecatedinit() has been deprecatedinit(concurrencyType:) is the designated initializer• Use PrivateQueueConcurrencyType or MainQueueConcurrencyType
![Page 80: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/80.jpg)
ConcurrencyConfinement is dead, long live queues
ConfinementConcurrencyType is deprecatedinit() has been deprecatedinit(concurrencyType:) is the designated initializer• Use PrivateQueueConcurrencyType or MainQueueConcurrencyType
NSManagedObjectContext Documentation developer.apple.com
What’s New in Core Data on iOS WWDC11
![Page 81: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/81.jpg)
Core Data Performance
![Page 82: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/82.jpg)
Apps Improve
Models get more complexStores get largerQueries get more interesting
![Page 83: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/83.jpg)
Apps Improve
Models get more complexStores get largerQueries get more interestingApps stay fast!
![Page 84: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/84.jpg)
Slow Can Be Surprising
Scale differs between development and productionThe simulator is faster than the device
![Page 85: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/85.jpg)
Slow Can Be Surprising
Scale differs between development and productionThe simulator is faster than the deviceUsers use devices in production
![Page 86: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/86.jpg)
Find Problems Before They Find YouPredicting the future with tools
![Page 87: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/87.jpg)
Relationship Faults
![Page 88: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/88.jpg)
Relationship Faults
![Page 89: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/89.jpg)
Relationship Faults
![Page 90: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/90.jpg)
Relationship FaultsPrefetch the objects you’re going to use
var recipeRequest = NSFetchRequest(entityName:"Recipe")
let sortDescriptor = NSSortDescriptor(key:"name", ascending: true) recipeRequest.sortDescriptors = [sortDestcriptor]
recipeRequest.relationshipKeyPathsForPrefetching = ["type"]
context.executeFetchRequest(recipeRequest)
![Page 91: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/91.jpg)
Relationship Faults
![Page 92: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/92.jpg)
Relationship Faults
![Page 93: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/93.jpg)
Relationship FaultsPrefetch the objects you’re going to use
var ingredientRequest = NSFetchRequest(entityName:"Ingredient")
ingredientRequest.predicate = NSPredicate(format:"recipe = %@", argumentArray:[recipe])
context.executeFetchRequest(ingredientRequest)
![Page 94: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/94.jpg)
Large Fetches
![Page 95: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/95.jpg)
Large Fetches
![Page 96: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/96.jpg)
Large Fetches
![Page 97: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/97.jpg)
Large Fetches
![Page 98: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/98.jpg)
Large FetchesTake advantage of batching
var recipeRequest = NSFetchRequest(entityName:"Recipe")
let sortDescriptor = NSSortDescriptor(key:"name", ascending: true) recipeRequest.sortDescriptors = [sortDestcriptor]
recipeRequest.fetchBatchSize = 30
context.executeFetchRequest(recipeRequest)
![Page 99: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/99.jpg)
Complex Fetches-com.apple.CoreData.SQLDebug 1
Larger time/count ratio
![Page 100: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/100.jpg)
Complex Fetches-com.apple.CoreData.SQLDebug 1
Larger time/count ratio
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID )) CoreData: annotation: sql connection fetch time: 0.0766s CoreData: annotation: total fetch execution time: 0.0786s for 85 rows.
![Page 101: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/101.jpg)
Complex Fetches-com.apple.CoreData.SQLDebug 1
Larger time/count ratio
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID )) CoreData: annotation: sql connection fetch time: 0.0766s CoreData: annotation: total fetch execution time: 0.0786s for 85 rows.
CoreData: annotation: Connecting to sqlite database file at "/Users/numist/… …
![Page 102: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/102.jpg)
Complex FetchesEXPLAIN QUERY PLAN
$ sqlite3 "/Users/numist/…/Recipes.sqlite" sqlite>
![Page 103: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/103.jpg)
Complex FetchesEXPLAIN QUERY PLAN
$ sqlite3 "/Users/numist/…/Recipes.sqlite" sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID ));
![Page 104: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/104.jpg)
Complex FetchesEXPLAIN QUERY PLAN
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID )); sele order from deta ---- ------------- ---- ---- 0 0 0 SCAN TABLE ZRECIPE AS t0 0 0 0 EXECUTE LIST SUBQUERY 1 1 0 0 SCAN TABLE ZRECIPE AS n1_t0 1 0 0 USE TEMP B-TREE FOR GROUP BY
![Page 105: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/105.jpg)
Complex FetchesEXPLAIN QUERY PLAN
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID )); sele order from deta ---- ------------- ---- ---- 0 0 0 SCAN TABLE ZRECIPE AS t0 0 0 0 EXECUTE LIST SUBQUERY 1 1 0 0 SCAN TABLE ZRECIPE AS n1_t0 1 0 0 USE TEMP B-TREE FOR GROUP BY
![Page 106: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/106.jpg)
Complex FetchesEXPLAIN QUERY PLAN
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID )); sele order from deta ---- ------------- ---- ---- 0 0 0 SCAN TABLE ZRECIPE AS t0 0 0 0 EXECUTE LIST SUBQUERY 1 1 0 0 SCAN TABLE ZRECIPE AS n1_t0 1 0 0 USE TEMP B-TREE FOR GROUP BY
![Page 107: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/107.jpg)
Complex FetchesEXPLAIN QUERY PLAN
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID )); sele order from deta ---- ------------- ---- ---- 0 0 0 SCAN TABLE ZRECIPE AS t0 0 0 0 EXECUTE LIST SUBQUERY 1 1 0 0 SCAN TABLE ZRECIPE AS n1_t0 1 0 0 USE TEMP B-TREE FOR GROUP BY
![Page 108: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/108.jpg)
Complex FetchesEXPLAIN QUERY PLAN
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID )); sele order from deta ---- ------------- ---- ---- 0 0 0 SCAN TABLE ZRECIPE AS t0 0 0 0 EXECUTE LIST SUBQUERY 1 1 0 0 SCAN TABLE ZRECIPE AS n1_t0 1 0 0 USE TEMP B-TREE FOR GROUP BY
![Page 109: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/109.jpg)
Complex FetchesLarge fetches benefit from indexes
![Page 110: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/110.jpg)
Complex FetchesLarge fetches benefit from indexes
![Page 111: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/111.jpg)
Complex FetchesLarge fetches benefit from indexes
![Page 112: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/112.jpg)
Complex FetchesVerify a better plan
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID )); sele order from deta ---- ------------- ---- ---- 0 0 0 SCAN TABLE ZRECIPE AS t0 0 0 0 EXECUTE LIST SUBQUERY 1 1 0 0 SCAN TABLE ZRECIPE AS n1_t0 USING COVERING INDEX ZRECIPE_ZSOURCE_ZEXTERNALID
![Page 113: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/113.jpg)
Complex FetchesIrreducible complexity
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZEXTERNALID, t0.ZINSTRUCTIONS, t0.ZNAME, t0.ZOVERVIEW, t0.ZPREPTIME, t0.ZSOURCE, t0.ZTHUMBNAILIMAGE, t0.ZIMAGE, t0.ZTYPE FROM ZRECIPE t0 WHERE NOT ( t0.Z_PK IN (SELECT n1_t0.Z_PK FROM ZRECIPE n1_t0 GROUP BY n1_t0.ZSOURCE, n1_t0.ZEXTERNALID )); sele order from deta ---- ------------- ---- ---- 0 0 0 SCAN TABLE ZRECIPE AS t0 0 0 0 EXECUTE LIST SUBQUERY 1 1 0 0 SCAN TABLE ZRECIPE AS n1_t0 USING COVERING INDEX ZRECIPE_ZSOURCE_ZEXTERNALID
![Page 114: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/114.jpg)
Complex FetchesIrreducible complexity
Get off the main thread• Private queue context• NSAsynchronousFetchRequest
![Page 115: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/115.jpg)
Look for Problem Patterns
Relationship faults• Lots of small queries slow down your app
Large fetches• Make Core Data do the work
Complex fetches• Add indices and try more powerful predicates• Avoid blocking UI threads
![Page 116: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/116.jpg)
http://bugreport.apple.com
Bugs• Sample app bonus
Feature requestsEnhancement ideasPerformance issues• Sample store bonus
Documentation improvements
![Page 117: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/117.jpg)
Developer Portaldeveloper.apple.com
Documentation and Sample Codedeveloper.apple.com/library
Developer Forumsdeveloper.apple.com/forums
Developer Technical Supportdeveloper.apple.com/support/technical
More Information
![Page 118: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/118.jpg)
Core Data Lab Frameworks Lab C Thursday 3:30PM
Core Data Lab Frameworks Lab E Friday 10:00AM
Related Labs
![Page 119: What’s New in Core Data - Apple Inc. · Confinement is dead, long live queues ... What’s New in Core Data on iOS WWDC11. Core Data Performance. Apps Improve Models get more complex](https://reader033.fdocuments.net/reader033/viewer/2022060319/5f0cc4a87e708231d4370909/html5/thumbnails/119.jpg)