Date post: | 15-Feb-2017 |
Category: |
Software |
Upload: | yi-shou-chen |
View: | 56 times |
Download: | 0 times |
Core Data
Neo
OutlineCore Data Introduction Core Data Stack Core Data Operation Core Data Concurrency Experience
Reference:https://developer.apple.com/library/watchos/documentation/Cocoa/Conceptual/CoreData/index.html
Core Data IntroductionWhat is the Core Data
Core Data is not RDBS, it’s ORM It’s a framework to manage the model layer object, not a DBMS Intelligent and efficiency data management tools
Advantage in Core Data Efficiency data r/w, ex:lazy loading, faulting pre-fetching, etc… Intelligent data schema upgrade & migration Schema version control Data editing in memory, save changes back to store
Simple to know stack vs. RMDB
Core Data Stack
tables
table configuration
file.sql
files
Core Data Stack
Establish a Core Data stack
Core Data Stack
Core Data Stack
Core Data Operation
Core Data Operation
Request data from fetchResultsController
Request data from executeFetchRequest
Core Data OperationInsert new data to MOC
Delete data from MOC
Modify data in MOC
Save change from MOC to PSC
Core Data OperationIf you set delegate to fetchResultsController, then you can monitor what change in the MOC
Core Data ConcurrencyCore data supports concurrency operation and easy to implement, there are two ways
Child Managed Object Context Another Managed Object Context but same Persistent Store Coordinator (Not recommend anymore)
What time does we need it a lot of executions in core data, ex:insert, delete, or update A complex query which need a long time App is almost waiting for core data access
The advantage is performance improvement when App has data access performance issue.
Core Data ConcurrencyShouldn’t easy to try it if no performance issue
Program would be more complex, and access data operation too. Managed Object Context is not thread-safe Data is needed to care about conflict and merge. Data is may not synced when miss some operations
Core Data ConcurrencySelect one concurrencyType when create MOC
NSConfinementConcurrencyType Default type for backward, it’s for keeping MOC is accessed in the thread which it is created. (Deprecated in iOS 9.0) NSPrivateQueueConcurrencyType (child MOC) Create a private queue by GCD, it won’t block mainThread. NSMainQueueConcurrencyType (main MOC) MOC is accessed in mainThread, it would block UI.
Core Data ConcurrencyChild Concurrency architecture
Core Data Concurrency
Asynchronous Saving When you have heavy I/O in core data
Core Data ConcurrencyOperation of child MOC is the same as parent MOC, but there are something important
Pass the MO to different MOC that is forbidden Child MOC save just update data of parent MOC, it won’t save data to disk. Before child MOC save, data of parent MOC won’t have any change, also parent MOC can’t find data which belong to child MOC.
Is only the architecture? NO, but I don’t think that need more concurrency architectures in the most normal cases.
Experience
We got the Core Data fundamental now, but something which need to be known in my experience
ExperiencePass MO between different contexts
We can’t pass MO directly Use NSManagedObjectID to access MO from persistentStore or memory. MO status maybe is not consistentex: MO is changed in child MOC without save, and the change won’t affect the MO in parent MOC.
NSManagedObjectID has two status PersistentID The MO is saved back to persistent store, and can be accessed anywhere by objectID TemporaryIDThe MO is no saved before, and it can’t be accessed from context. After saved MO, you can’t find object by the ID!! BTW, you can check the ID status by isTemporaryID
ExperienceThere are three methods access MO by objectID
objectWithID Always return a MO, whatever it has data or not. It can accessed parent’s temp MO in child, but that’s a new one.Don’t use return value as Bool, it always get a empty data MO!! objectRegisteredForIDReturn MO if existed in MOC, otherwise return nil. Ex: a MO saved in parent MOC, the method can’t access it in child MOC. existingObjectWithID Return MO if it has been saved before, otherwise return nil.
ExperienceMO has some status properties
isFault, isDeleted, faultingState, isInserted, isUpdated, isTemporaryID, hasChanges These status can be checked what they do in context These status would be reset after saved
isDeleted If delete MO and it’s no saved before, then the MO would be nil directly If delete MO and it’s saved before, then only isDelete property would be changed After deleted MO, it always existed if it was saved before!!
Check MO is deleted Sometimes isDeleted can’t really know MO is deleted or not Using existingObjectWithID and managedObjectContextCheck condition should be: (MO.isDelete || ![MOC exist..ID:MOID] || !MO.managedObjectContext)
ExperienceMO Update
Without save, change MO in the child MOC won’t affect attribute/status MO in the parent MOC, otherwise, too. Without save, insert MO in the child MOC won’t affect MO in the parent MOC, otherwise, child MOC would be affected.
You can try core data operation in my testbed https://github.com/flamelad/CoreDataTestbed
Q & A