Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Core Data: overall review by Yurii Boyko

Core Data: overall review by Yurii Boyko

Core Data: overall review by Yurii Boyko

GDG Ternopil

November 19, 2015
Tweet

More Decks by GDG Ternopil

Other Decks in Programming

Transcript

  1. What is Core Data? • Technical answer: Core Data is

    an object graph management and persistence framework • Core Data builds on that you’re storing data in objects, Core Data keeps track of the objects and their relationships to each other
  2. Core Data benefits Easy to Use It works out-of-the- box:

    • it embed in iOS • designed for UITableView • easy to save properties for objects Incredible Speed Core Data is the most powerful framework. If you use MagicalRecord, there is no need in writing a lot of code lines. Great Management Core Data manages all the relationships, as well as updates for delegates. It also keeps your controller in the loop of all the changes in the database.
  3. We Use Core Data for the Following Purposes Dos: •

    apps with a relatively simple object graph • simple apps with a lot of data and framework usage as a requirement • fast and quality code writing Don'ts: • large data storage (BLOBs) • complex requests or data • simple apps where we can use NSArray
  4. NSPropertyDescription A property is a quality of an entity. Each

    Property is created as a column in a SQLite store.
  5. NSEntityDescription The entity defines what will be managed by Core

    Data. It's a description of something you want to store.
  6. NSManagedObjectModel The Managed Object Model contains: • a detailed description

    of an application's data types • the Model contains ◦ Entities ◦ Properties ◦ Fetch Requests
  7. A Managed Object represents one record in a data file.

    Each Managed Object "hosts" the Entity which gives it the properties of a certain data type. NSManagedObject
  8. Faults NSManagedObject (lldb) po conference <SMConference: 0x1740b3c20> (entity: SMConference; id:

    0xd0000000a4300000 <x-coredata: //983E8C0C-E03A-47DE-988A-0E197B341451/SMConference/p10508> ; data: <fault>)
  9. NSManagedObjectContext The Context stores and retrieves all user data transparently,

    provides undo and redo, as well as "revert to saved" functionality.
  10. NSManagedObjectContext: Useful Methods • -save: writes changed data to the

    data file • -deleteObject: marks a Managed Object for deletion, but object doesn't actually go away until the Context's changes are committed • -rollback: reverts to the content of the data file • -undoManager: returns the NSUndoManager instance that in use in Context • -executeFetchRequest: error: runs a Fetch Request and returns any matching objects
  11. NSPersistentStoreCoordinator Even single-document Core Data applications: • store data from

    different locations on disk • load data from different locations on disk The Persistent Store Coordinator handles management of actual data files on disk
  12. NSPersistentDocument • provides methods to access a document-wide NSManagedObjectContext object.

    • provides default implementations of methods to read and write files using the persistence framework.
  13. NSFetchRequest A Fetch Request is • a self-contained query •

    which is sent to the Managed Object Context The Context evaluates the request and returns the results in the form of an NSArray.
  14. NSFetchRequest NSFetchRequest • -setEntity: specifies the type of objects you'd

    like to fetch (required) • -setPredicate: specify a Predicate to constrain the results to a certain set of criteria • -setFetchLimit: sets the maximum number of objects to fetch • -setSortDescriptors: provides an array of NSSortDescriptors to specify the order the results should be return
  15. NSPredicate Predicates are used to specify search criteria, and are

    made up of expressions and nested predicates. Used in: • arrays • Core Data • strings
  16. NSPredicate: Real Use NSString *regex = @"[a-zA-Z0-9\\+\\.\\_]{1,64}"; NSPredicate *s =

    [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; BOOL isTextOnly = [s evaluateWithObject:self];
  17. Core Data 2015-11-18 20:46:02.502 GDGTernopilSpeakersManager[2166:585699] CoreData: sql: INSERT INTO ZSMPRESENTATION(Z_PK,

    Z_ENT, Z_OPT, ZCONFERENCE, ZSPEAKER, ZCOMMENTS, ZMINUTES, ZPRESENTATIONID, ZTITLE) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) 2015-11-18 20:46:03.224 GDGTernopilSpeakersManager[2166:585699] CoreData: annotation: sql execution time: 0.0002s 2015-11-18 20:46:05.024 GDGTernopilSpeakersManager[2166:585699] CoreData: annotation: sql connection fetch time: 0.4530s 2015-11-18 20:46:05.026 GDGTernopilSpeakersManager[2166:585699] CoreData: annotation: total fetch execution time: 0.4555s for 12000 rows.