Top Banner
Core Data Neo
23

Core Data Introduction

Feb 15, 2017

Download

Software

Yi-Shou Chen
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: Core Data Introduction

Core Data

Neo

Page 2: Core Data Introduction

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

Page 3: Core Data Introduction

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

Page 4: Core Data Introduction

Simple to know stack vs. RMDB

Core Data Stack

tables

table configuration

file.sql

files

Page 5: Core Data Introduction

Core Data Stack

Page 6: Core Data Introduction

Establish a Core Data stack

Core Data Stack

Page 7: Core Data Introduction

Core Data Stack

Page 8: Core Data Introduction

Core Data Operation

Page 9: Core Data Introduction

Core Data Operation

Request data from fetchResultsController

Request data from executeFetchRequest

Page 10: Core Data Introduction

Core Data OperationInsert new data to MOC

Delete data from MOC

Modify data in MOC

Save change from MOC to PSC

Page 11: Core Data Introduction

Core Data OperationIf you set delegate to fetchResultsController, then you can monitor what change in the MOC

Page 12: Core Data Introduction

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.

Page 13: Core Data Introduction

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

Page 14: Core Data Introduction

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.

Page 15: Core Data Introduction

Core Data ConcurrencyChild Concurrency architecture

Page 16: Core Data Introduction

Core Data Concurrency

Asynchronous Saving When you have heavy I/O in core data

Page 17: Core Data Introduction

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.

Page 18: Core Data Introduction

Experience

We got the Core Data fundamental now, but something which need to be known in my experience

Page 19: Core Data Introduction

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

Page 20: Core Data Introduction

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.

Page 21: Core Data Introduction

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)

Page 22: Core Data Introduction

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

Page 23: Core Data Introduction

Q & A