SlideShare a Scribd company logo
Adventures in
Multithreaded Core Data
    CocoaheadsBE - Zoersel, 2012-09-24
Introduction
Adventures in Multithreaded Core Data
Tom Adriaenssen
I love...
‣ ... my wife
‣ ... my 4 kids
‣ ... to code
‣ ... to play a game of squash
‣ ... good beer
I open sourced...
... some code:

‣ IIViewDeckController: “An
  implementation of the
  sliding functionality found in
  the Path 2.0 or Facebook
  iOS apps.”

‣ IIDateExtensions

‣ IIPopoverStatusItem

See: http://github.com/inferis
I made...
... some apps:




         Butane                     Drash
     http://getbutane.com          http://dra.sh

         Hi, @10to1!
Butane
                        Campfire client for iOS

‣ Official Campfire client
  kinda sucks, so we
  rolled our own

‣ Somewhat concurrent
  app

‣ Uses Core Data

‣ Together with 10to1
This presentation is about
      what I learned
  while coding Butane.
Agenda
Agenda

‣ Core Data Primer
‣ MagicalRecord
‣ Concurrency problems
‣ Concurrency solutions
Core Data Primer
What is: Core Data?
                ‣ Per the documentation:
                ‣ The  Core  Data  framework  provides  
                  generalized  and  automated  solutions  
                  to  common  tasks  associated  with  
                  object  life-­‐cycle  and  object  graph  
                  management,  including  persistence.




http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/CoreData/Articles/cdTechnologyOverview.html#//apple_ref/doc/uid/TP40009296-SW1
Wait, what?
What isn’t: Core Data?

‣ It’s not an RDBMS.
‣ If you want a database and SQL, use
  Sqlite:
What isn’t: Core Data?

‣ It’s not just an ORM (Object Relational
  Mapper)
‣ It may look like there’s SQL under the
  hood, but that’s not necessarily the case.
So, what is it then?
Core Data provides:
‣ persistence
‣ change tracking
‣ relations (object graph)
‣ lazy loading (“faulting”)
‣ validation
‣ works well with Cocoa (KVO, KVC)
Basically:

‣ A system to store data
‣ Persistence agnostic (local storage,
  iCloud, AFIncrementalStore, ...)
‣ No need to write SQL to query
‣ You can keep to Objective-C
Your tools:
‣ NSPersistentStore
‣ NSManagedObjectContext
‣ NSManagedObject
‣ NSManagedObjectID  
‣   NSFetchRequest
‣   NSEntityDescription
‣   NSPredicate
‣   NSSortDescription
A picture says a 1000 words...
MagicalRecord
MagicalRecord
           ‣ Writing Core Data code is tedious.
           ‣ You need quite a bit of boilerplate code
             to do something simple:

NSManagedObjectContext  *moc  =  [self  managedObjectContext];
NSEntityDescription  *entityDescription  =  [NSEntityDescription  entityForName:@"Employee"  inManagedObjectContext:moc];
NSFetchRequest  *request  =  [NSFetchRequest  new];
[request  setEntity:entityDescription];
  
NSSortDescriptor  *sortDescriptor  =  [[NSSortDescriptor  alloc]  initWithKey:@"firstName"  ascending:YES];
[request  setSortDescriptors:@[sortDescriptor]];
  
NSError  *error;
NSArray  *array  =  [moc  executeFetchRequest:request  error:&error];

if  (array)
{
           //  display  items  (eg  in  table  view)
}
else  {
        //  Deal  with  error...
}
MagicalRecord
‣ MagicalRecord tries to solve this.


‣ = ActiveRecord pattern for Core Data.
‣ Encapsulates the tediousness of plain
  Core Data code.
MagicalRecord
           ‣ Writing MagicalRecord enable code is
             tedious no more:
           ‣ That same example is now this:


NSManagedObjectContext  *moc  =  [self  managedObjectContext];
NSArray  *array  =  [Employee  MR_findAllSortedBy:@"firstname"  ascending:YES  inContext:context];

if  (array)
{
           //  display  items  (eg  in  table  view)
}
else  {
        //  Deal  with  error...
}
Adventures in Multithreaded Core Data
Great, isn’t it?
MagicalRecord
+ write less code
+ your code becomes more readable
+ good for apps requiring simple storage scenarios (=most
  apps, probably)
+ hides complexity
-   hides complexity
-   easy to start, but diving deeper becomes harder
- uses defaults that are suboptimal in a multithreaded app
- concurrency errors and issues are subtle
MagicalRecord
‣      used MagicalRecord from the start
‣ Took me a while to find out the problems
  I was having were related to the
  complexity hiding
‣ The defaults are okay only for so much
  app complexity
MagicalRecord
‣ That said: Magical Record is great.
‣ It will speed up your Core Data
  development by several factors.
‣ Also: take a look at mogenerator:
 ‣   http://rentzsch.github.com/mogenerator/

 ‣   or: brew  install  mogenerator
MagicalRecord

‣ So I threw it out.
MagicalRecord

‣ So I threw it out.
‣ But not completely.
MagicalRecord

‣ So I threw it out.
‣ But not completely.
‣ More about that later.
The problems described hereafter only apply
to the persistent stores with external backing
             (for example: sqlite).
Concurrency: problems
Problems?
Problems?

‣ Core Data Objects are not thread safe.
Problems?

‣ Core Data Objects are not thread safe.
‣ In essence: you can’t share them across
  threads (except for NSManagedObjectID).
Problems?

‣ Core Data Objects are not thread safe.
‣ In essence: you can’t share them across
  threads (except for NSManagedObjectID).
‣ Core Data locks objects, even for read
  operations.
Object storage is locked
for read operations, too
‣ Objects used to power the UI must be
  fetched on the UI thread.
‣ Heavy/complex fetch requests (queries)
  block the UI thread while fetching the
  objects. You don’t want that.
Objects aren’t supposed to
be shared between threads
‣ The NSManagedObjectContext “locks” an
  object when you read one of its properties.
‣ This can cause a deadlock when you do
  access the same data from 2 threads.
‣ Reason: faulting support can change the
  object even while just reading from it.
‣ You can’t turn it off.
Adventures in Multithreaded Core Data
Luckily, we can fix or workaround these
               problems.
Concurrency: solutions
Keep to your thread
Keep to your thread


‣ pre-iOS5: use thread confinement
Keep to your thread


‣ pre-iOS5: use thread confinement
‣ iOS5 and later: use nested contexts
Thread confinement
‣ In essence: keep an NSManagedObjectContext per
  thread
‣ Be very careful when going from one thread to
  another.


‣ MagicalRecord tries to hide this from you:
  ‣ It automatically provides a context for each thread
  ‣ This is a bit counterintuitive since you start mixing
    objects across threads quite easily.
Thread confinement




     Image source: Cocoanetics
Nested contexts
‣   Introduced in iOS5
‣   Uses Grand Central Dispatch and dispatch queues
‣   Core Data manages threading for you
‣   Better than thread confinement
    ‣   more straightforward
    ‣   more flexible


‣   MagicalRecord hides this from you, too.
    ‣   Automatically switches to dispatch queues on iOS5 even
        though the API remains the same.
Nested contexts




    Image source: Cocoanetics
Nested contexts
‣ NSManagedObjectContext = cheap
‣ You can nest contexts
‣ Each context has its private dispatch
  queue
‣ No manual thread synchronization
  necessary
Queue types
    ‣ NSConfinementConcurrencyType
        ‣     The old way (thread confinement)

    ‣ NSPrivateQueueConcurrencyType
        ‣     The context has its own private dispatch queue

    ‣ NSMainQueueConcurrencyType
        ‣     The context is associated with the main queue (or runloop, or UI
              thread)


parentMoc  =  [[NSManagedObjectContext  alloc]  initWithConcurrencyType:NSMainQueueConcurrencyType];
[parentMoc  setPersistentStoreCoordinator:coordinator];

moc  =  [[NSManagedObjectContext  alloc]  initWithConcurrencyType:NSPrivateQueueConcurrencyType];
moc.parentContext  =  parentMoc;
Thread safe?
   ‣ performBlock:   
     performBlockAndWait:
   ‣ Run a block you provide on the queue
     associated with the context.
        ‣ Object access in the block is thread safe
[context  performBlockAndWait:^{
        for  (Room*  room  in  [Room  findAllInContext:context])  {
                room.joinedUsers  =  [NSSet  set];
                room.knowsAboutJoinedUsersValue  =  NO;
                room.unreadCountValue  =  0;
                room.status  =  @"";
        }
        NSError*  error  =  nil;
        [context  save:&error];
}];
performBlock, and wait
-­‐  (void)performBlock:(void  (^)())block;
   ‣   executes the block on the context dispatch queue as soon as possible

   ‣   nonblocking call

   ‣   code will not execute immediately



-­‐  (void)performBlockAndWait:(void  (^)())block;
   ‣   executes the block on the context dispatch queue immediately

   ‣   blocks the current execution until block is done

   ‣   can cause a deadlock (if you’re already running code on the same
       queue)
When to use what?
‣   performBlock:
    ‣   For actions which are “on their own”
    ‣   Consider the code in the block a Unit Of Work
    ‣   Best for save operations
    ‣   Useful for long fetches (use callbacks)


‣   performBlockAndWait:
    ‣   When you need stuff immediately
    ‣   Good for small fetches or “standalone” saves
How is this better than
 thread confinement?
‣ No manual thread handling, Core Data
  handles it for you.
‣ More flexible: as long as you access
  managed objects in the correct context
  using performBlock: you’re pretty safe
 ‣ also applies to the main/UI thread! (unless
   you’re sure you’re on the main thread)
Saving nested contexts
‣ Saves are only persisted one level deep.
‣ Parent contexts don’t pull changes from
  child contexts.
‣ Child contexts don’t see changes by
  parent contexts.
 ‣   Make them plenty and short-lived
How to nest contexts

‣ 2 approaches:
 ‣ root context = NSMainQueueConcurrencyType
 ‣ root context = NSPrivateQueueConcurrencyType
Root = Main
                              ‣ Many child contents
                                with private queues

                              ‣ Root context on main
                                queue

                              ‣ Actual persistence
                                happens on main
                                queue, could block the
                                UI

  Image source: Cocoanetics
Root = Private
                         ‣ Root context with private
                           queue

                         ‣ Many child contents with
                           private queues

                         ‣ context on main queue is
                           child of root

                         ‣ Actual persistence
                           happens in background
                           (does not block the UI)

   Image source: Cocoanetics
What problems did we
    have again?
What problems did we
    have again?

‣ No sharing of NSManagedObjects
  between threads.
What problems did we
     have again?

‣ No sharing of NSManagedObjects
  between threads.
‣ Context locking
Sharing: solution
‣ Pass only NSManagedObjectIDs to
  other threads, not objects.
‣ Refetch the object on a different thread
  or queue to work with it.
‣ Don’t forget to save the ‘original’ object
  first before working with it on the second
  thread or queue.
Complex queries
‣   Use the same technique for complex or large queries.
    1. do the heavy lifting in the background
    2. pass list of NSManagedObjectIDs to another thread
       (e.g. UI thread).
    3. load objects as faults, and let Core Data fault them in
       when you need them (e.g. when accessing a property)


‣   That’s lot of requests, but this is actually more performant
    and desirable in most cases.
Locking: solution
‣ Use child contexts with their own
  dispatch queues.
‣ Use: performBlock: and
  performBlockAndWait:  carefully.
‣ Deadlocks still possible, especially with
  performBlockAndWait:
A word about
 NSManagedObjectIDs
‣ Two types of IDs:
 ‣ temporary
   ‣ when the object hasn’t been persisted to a
     store

 ‣ permanent
   ‣ when the object has been persisted to a
     store
A word about
 NSManagedObjectIDs
‣ Subtle bug: temporary IDs from a non-root
  context don’t get updated to permanent
  IDs when saving in the root context
‣ The object is saved just fine, but the ID is
  not updated correctly.
‣ When passing these around to other
  contexts after saving: you won’t find the
  object in another child context!
A word about
 NSManagedObjectIDs

‣ To the rescue:
  -­‐  (BOOL)obtainPermanentIDsForObjects:
  (NSArray  *)objects  error:(NSError  
  **)error;
A word about
  NSManagedObjectIDs
   -­‐  (BOOL)obtainPermanentIDsForObjects:(NSArray  
   *)objects  error:(NSError  **)error;



‣ Transforms objects with temporary IDs to permanent IDs
  (through the persistent store of the root context).
‣ Do this when creating a managed object and you’re
  safe.
‣ Obtaining permanentIDs is batched, so the performance
  hit is not that high
MagicalRecord
‣ I still use MagicalRecord:
 ‣ Reduced form: no more “automatic”
   context handling --> dangerous!
 ‣ Added some extra sauce to work with
   the nested contexts.
 ‣ The methods MR supplies still allow for
   a speedup when coding.
Useful References


‣   Nested context release notes: http://developer.apple.com/library/mac/#releasenotes/
    DataManagement/RN-CoreData/_index.html
‣   Magical Record: https://github.com/magicalpanda/MagicalRecord
‣   Mogenerator: http://rentzsch.github.com/mogenerator/
‣   A good read on Cocoanetics: http://www.cocoanetics.com/2012/07/multi-context-coredata/
‣   Core data programming guide: http://developer.apple.com/library/mac/#documentation/
    cocoa/Conceptual/CoreData/cdProgrammingGuide.html
‣   Butane: http://getbutane.com
Thanks for listening.


Questions? Contact me:

Twitter: @inferis
App.Net: @inferis
E-mail: tom@interfaceimplementation.be
vCard: http://inferis.org

More Related Content

What's hot (20)

PDF
Beginning icloud development - Cesare Rocchi - WhyMCA
Whymca
 
PPTX
Scalable network applications, event-driven - Node JS
Cosmin Mereuta
 
ODP
Polyglot persistence with Spring Data
Corneil du Plessis
 
PPTX
Getting Started with Datatsax .Net Driver
DataStax Academy
 
PDF
Mongo performance tuning: tips and tricks
Vladimir Malyk
 
PDF
Modern Android app library stack
Tomáš Kypta
 
PPTX
MongoDB: tips, trick and hacks
Scott Hernandez
 
PDF
Getting started with node JS
Hamdi Hmidi
 
PDF
Hazelcast
oztalip
 
PDF
Nuvola: a tale of migration to AWS
Matteo Moretti
 
KEY
iOS5 NewStuff
deenna_vargilz
 
PPTX
Whats New for WPF in .NET 4.5
Rainer Stropek
 
PPT
Java. Explicit and Implicit Wait. Testing Ajax Applications
Марія Русин
 
PPTX
MongoDB Performance Tuning and Monitoring
MongoDB
 
TXT
Birhanu distributive assignment
university
 
PDF
Pycon 2012 Apache Cassandra
jeremiahdjordan
 
PDF
Introduction to Node.js Platform
Naresh Chintalcheru
 
PPTX
Getting started with Elasticsearch and .NET
Tomas Jansson
 
PPT
Zend framework 03 - singleton factory data mapper caching logging
Tricode (part of Dept)
 
KEY
Whats new in iOS5
Paul Ardeleanu
 
Beginning icloud development - Cesare Rocchi - WhyMCA
Whymca
 
Scalable network applications, event-driven - Node JS
Cosmin Mereuta
 
Polyglot persistence with Spring Data
Corneil du Plessis
 
Getting Started with Datatsax .Net Driver
DataStax Academy
 
Mongo performance tuning: tips and tricks
Vladimir Malyk
 
Modern Android app library stack
Tomáš Kypta
 
MongoDB: tips, trick and hacks
Scott Hernandez
 
Getting started with node JS
Hamdi Hmidi
 
Hazelcast
oztalip
 
Nuvola: a tale of migration to AWS
Matteo Moretti
 
iOS5 NewStuff
deenna_vargilz
 
Whats New for WPF in .NET 4.5
Rainer Stropek
 
Java. Explicit and Implicit Wait. Testing Ajax Applications
Марія Русин
 
MongoDB Performance Tuning and Monitoring
MongoDB
 
Birhanu distributive assignment
university
 
Pycon 2012 Apache Cassandra
jeremiahdjordan
 
Introduction to Node.js Platform
Naresh Chintalcheru
 
Getting started with Elasticsearch and .NET
Tomas Jansson
 
Zend framework 03 - singleton factory data mapper caching logging
Tricode (part of Dept)
 
Whats new in iOS5
Paul Ardeleanu
 

Similar to Adventures in Multithreaded Core Data (20)

PDF
Qardio experience with Core Data
Dmitrii Ivanov
 
PPT
Connecting to a REST API in iOS
gillygize
 
PDF
Core data intermediate Workshop at NSSpain 2013
Diego Freniche Brito
 
PDF
Core data WIPJam workshop @ MWC'14
Diego Freniche Brito
 
PDF
CoreData
Ali Akhtar
 
PDF
Core Data in Modern Times
Jorge Ortiz
 
KEY
iOSDevCamp 2011 Core Data
Chris Mar
 
PDF
CoreData - there is an ORM you can like!
Tomáš Jukin
 
PDF
Advanced Core Data
Make School
 
PPT
Core data orlando i os dev group
Andrew Kozlik
 
PDF
Core Data Introduction
Yi-Shou Chen
 
KEY
Core Data
Robert Brown
 
PDF
Intro to Core Data
Make School
 
PDF
Core data basic Workshop slides NSSpain 2013
Diego Freniche Brito
 
PDF
CoreData Best Practices (2021)
deeje cooley
 
PDF
Advanced Core Data - The Things You Thought You Could Ignore
Aaron Douglas
 
PDF
Elements for an iOS Backend
Laurent Cerveau
 
PPT
Core data optimization
Gagan Vishal Mishra
 
PDF
Core Data
Rinto Rapheal
 
PPTX
Core Data Performance Guide Line
Gagan Vishal Mishra
 
Qardio experience with Core Data
Dmitrii Ivanov
 
Connecting to a REST API in iOS
gillygize
 
Core data intermediate Workshop at NSSpain 2013
Diego Freniche Brito
 
Core data WIPJam workshop @ MWC'14
Diego Freniche Brito
 
CoreData
Ali Akhtar
 
Core Data in Modern Times
Jorge Ortiz
 
iOSDevCamp 2011 Core Data
Chris Mar
 
CoreData - there is an ORM you can like!
Tomáš Jukin
 
Advanced Core Data
Make School
 
Core data orlando i os dev group
Andrew Kozlik
 
Core Data Introduction
Yi-Shou Chen
 
Core Data
Robert Brown
 
Intro to Core Data
Make School
 
Core data basic Workshop slides NSSpain 2013
Diego Freniche Brito
 
CoreData Best Practices (2021)
deeje cooley
 
Advanced Core Data - The Things You Thought You Could Ignore
Aaron Douglas
 
Elements for an iOS Backend
Laurent Cerveau
 
Core data optimization
Gagan Vishal Mishra
 
Core Data
Rinto Rapheal
 
Core Data Performance Guide Line
Gagan Vishal Mishra
 
Ad

More from Inferis (6)

PDF
Practical auto layout
Inferis
 
PDF
Communicating with GIFs
Inferis
 
PDF
Autolayout primer
Inferis
 
PDF
Objective c runtime
Inferis
 
PPT
I Am A Switcher
Inferis
 
PPT
Dynamic Silverlight
Inferis
 
Practical auto layout
Inferis
 
Communicating with GIFs
Inferis
 
Autolayout primer
Inferis
 
Objective c runtime
Inferis
 
I Am A Switcher
Inferis
 
Dynamic Silverlight
Inferis
 
Ad

Recently uploaded (20)

PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 

Adventures in Multithreaded Core Data

  • 1. Adventures in Multithreaded Core Data CocoaheadsBE - Zoersel, 2012-09-24
  • 5. I love... ‣ ... my wife ‣ ... my 4 kids ‣ ... to code ‣ ... to play a game of squash ‣ ... good beer
  • 6. I open sourced... ... some code: ‣ IIViewDeckController: “An implementation of the sliding functionality found in the Path 2.0 or Facebook iOS apps.” ‣ IIDateExtensions ‣ IIPopoverStatusItem See: http://github.com/inferis
  • 7. I made... ... some apps: Butane Drash http://getbutane.com http://dra.sh Hi, @10to1!
  • 8. Butane Campfire client for iOS ‣ Official Campfire client kinda sucks, so we rolled our own ‣ Somewhat concurrent app ‣ Uses Core Data ‣ Together with 10to1
  • 9. This presentation is about what I learned while coding Butane.
  • 11. Agenda ‣ Core Data Primer ‣ MagicalRecord ‣ Concurrency problems ‣ Concurrency solutions
  • 13. What is: Core Data? ‣ Per the documentation: ‣ The  Core  Data  framework  provides   generalized  and  automated  solutions   to  common  tasks  associated  with   object  life-­‐cycle  and  object  graph   management,  including  persistence. http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/CoreData/Articles/cdTechnologyOverview.html#//apple_ref/doc/uid/TP40009296-SW1
  • 15. What isn’t: Core Data? ‣ It’s not an RDBMS. ‣ If you want a database and SQL, use Sqlite:
  • 16. What isn’t: Core Data? ‣ It’s not just an ORM (Object Relational Mapper) ‣ It may look like there’s SQL under the hood, but that’s not necessarily the case.
  • 17. So, what is it then? Core Data provides: ‣ persistence ‣ change tracking ‣ relations (object graph) ‣ lazy loading (“faulting”) ‣ validation ‣ works well with Cocoa (KVO, KVC)
  • 18. Basically: ‣ A system to store data ‣ Persistence agnostic (local storage, iCloud, AFIncrementalStore, ...) ‣ No need to write SQL to query ‣ You can keep to Objective-C
  • 19. Your tools: ‣ NSPersistentStore ‣ NSManagedObjectContext ‣ NSManagedObject ‣ NSManagedObjectID   ‣ NSFetchRequest ‣ NSEntityDescription ‣ NSPredicate ‣ NSSortDescription
  • 20. A picture says a 1000 words...
  • 22. MagicalRecord ‣ Writing Core Data code is tedious. ‣ You need quite a bit of boilerplate code to do something simple: NSManagedObjectContext  *moc  =  [self  managedObjectContext]; NSEntityDescription  *entityDescription  =  [NSEntityDescription  entityForName:@"Employee"  inManagedObjectContext:moc]; NSFetchRequest  *request  =  [NSFetchRequest  new]; [request  setEntity:entityDescription];   NSSortDescriptor  *sortDescriptor  =  [[NSSortDescriptor  alloc]  initWithKey:@"firstName"  ascending:YES]; [request  setSortDescriptors:@[sortDescriptor]];   NSError  *error; NSArray  *array  =  [moc  executeFetchRequest:request  error:&error]; if  (array) {   //  display  items  (eg  in  table  view) } else  {        //  Deal  with  error... }
  • 23. MagicalRecord ‣ MagicalRecord tries to solve this. ‣ = ActiveRecord pattern for Core Data. ‣ Encapsulates the tediousness of plain Core Data code.
  • 24. MagicalRecord ‣ Writing MagicalRecord enable code is tedious no more: ‣ That same example is now this: NSManagedObjectContext  *moc  =  [self  managedObjectContext]; NSArray  *array  =  [Employee  MR_findAllSortedBy:@"firstname"  ascending:YES  inContext:context]; if  (array) {   //  display  items  (eg  in  table  view) } else  {        //  Deal  with  error... }
  • 27. MagicalRecord + write less code + your code becomes more readable + good for apps requiring simple storage scenarios (=most apps, probably) + hides complexity - hides complexity - easy to start, but diving deeper becomes harder - uses defaults that are suboptimal in a multithreaded app - concurrency errors and issues are subtle
  • 28. MagicalRecord ‣ used MagicalRecord from the start ‣ Took me a while to find out the problems I was having were related to the complexity hiding ‣ The defaults are okay only for so much app complexity
  • 29. MagicalRecord ‣ That said: Magical Record is great. ‣ It will speed up your Core Data development by several factors. ‣ Also: take a look at mogenerator: ‣ http://rentzsch.github.com/mogenerator/ ‣ or: brew  install  mogenerator
  • 30. MagicalRecord ‣ So I threw it out.
  • 31. MagicalRecord ‣ So I threw it out. ‣ But not completely.
  • 32. MagicalRecord ‣ So I threw it out. ‣ But not completely. ‣ More about that later.
  • 33. The problems described hereafter only apply to the persistent stores with external backing (for example: sqlite).
  • 36. Problems? ‣ Core Data Objects are not thread safe.
  • 37. Problems? ‣ Core Data Objects are not thread safe. ‣ In essence: you can’t share them across threads (except for NSManagedObjectID).
  • 38. Problems? ‣ Core Data Objects are not thread safe. ‣ In essence: you can’t share them across threads (except for NSManagedObjectID). ‣ Core Data locks objects, even for read operations.
  • 39. Object storage is locked for read operations, too ‣ Objects used to power the UI must be fetched on the UI thread. ‣ Heavy/complex fetch requests (queries) block the UI thread while fetching the objects. You don’t want that.
  • 40. Objects aren’t supposed to be shared between threads ‣ The NSManagedObjectContext “locks” an object when you read one of its properties. ‣ This can cause a deadlock when you do access the same data from 2 threads. ‣ Reason: faulting support can change the object even while just reading from it. ‣ You can’t turn it off.
  • 42. Luckily, we can fix or workaround these problems.
  • 44. Keep to your thread
  • 45. Keep to your thread ‣ pre-iOS5: use thread confinement
  • 46. Keep to your thread ‣ pre-iOS5: use thread confinement ‣ iOS5 and later: use nested contexts
  • 47. Thread confinement ‣ In essence: keep an NSManagedObjectContext per thread ‣ Be very careful when going from one thread to another. ‣ MagicalRecord tries to hide this from you: ‣ It automatically provides a context for each thread ‣ This is a bit counterintuitive since you start mixing objects across threads quite easily.
  • 48. Thread confinement Image source: Cocoanetics
  • 49. Nested contexts ‣ Introduced in iOS5 ‣ Uses Grand Central Dispatch and dispatch queues ‣ Core Data manages threading for you ‣ Better than thread confinement ‣ more straightforward ‣ more flexible ‣ MagicalRecord hides this from you, too. ‣ Automatically switches to dispatch queues on iOS5 even though the API remains the same.
  • 50. Nested contexts Image source: Cocoanetics
  • 51. Nested contexts ‣ NSManagedObjectContext = cheap ‣ You can nest contexts ‣ Each context has its private dispatch queue ‣ No manual thread synchronization necessary
  • 52. Queue types ‣ NSConfinementConcurrencyType ‣ The old way (thread confinement) ‣ NSPrivateQueueConcurrencyType ‣ The context has its own private dispatch queue ‣ NSMainQueueConcurrencyType ‣ The context is associated with the main queue (or runloop, or UI thread) parentMoc  =  [[NSManagedObjectContext  alloc]  initWithConcurrencyType:NSMainQueueConcurrencyType]; [parentMoc  setPersistentStoreCoordinator:coordinator]; moc  =  [[NSManagedObjectContext  alloc]  initWithConcurrencyType:NSPrivateQueueConcurrencyType]; moc.parentContext  =  parentMoc;
  • 53. Thread safe? ‣ performBlock:   performBlockAndWait: ‣ Run a block you provide on the queue associated with the context. ‣ Object access in the block is thread safe [context  performBlockAndWait:^{        for  (Room*  room  in  [Room  findAllInContext:context])  {                room.joinedUsers  =  [NSSet  set];                room.knowsAboutJoinedUsersValue  =  NO;                room.unreadCountValue  =  0;                room.status  =  @"";        }        NSError*  error  =  nil;        [context  save:&error]; }];
  • 54. performBlock, and wait -­‐  (void)performBlock:(void  (^)())block; ‣ executes the block on the context dispatch queue as soon as possible ‣ nonblocking call ‣ code will not execute immediately -­‐  (void)performBlockAndWait:(void  (^)())block; ‣ executes the block on the context dispatch queue immediately ‣ blocks the current execution until block is done ‣ can cause a deadlock (if you’re already running code on the same queue)
  • 55. When to use what? ‣ performBlock: ‣ For actions which are “on their own” ‣ Consider the code in the block a Unit Of Work ‣ Best for save operations ‣ Useful for long fetches (use callbacks) ‣ performBlockAndWait: ‣ When you need stuff immediately ‣ Good for small fetches or “standalone” saves
  • 56. How is this better than thread confinement? ‣ No manual thread handling, Core Data handles it for you. ‣ More flexible: as long as you access managed objects in the correct context using performBlock: you’re pretty safe ‣ also applies to the main/UI thread! (unless you’re sure you’re on the main thread)
  • 57. Saving nested contexts ‣ Saves are only persisted one level deep. ‣ Parent contexts don’t pull changes from child contexts. ‣ Child contexts don’t see changes by parent contexts. ‣ Make them plenty and short-lived
  • 58. How to nest contexts ‣ 2 approaches: ‣ root context = NSMainQueueConcurrencyType ‣ root context = NSPrivateQueueConcurrencyType
  • 59. Root = Main ‣ Many child contents with private queues ‣ Root context on main queue ‣ Actual persistence happens on main queue, could block the UI Image source: Cocoanetics
  • 60. Root = Private ‣ Root context with private queue ‣ Many child contents with private queues ‣ context on main queue is child of root ‣ Actual persistence happens in background (does not block the UI) Image source: Cocoanetics
  • 61. What problems did we have again?
  • 62. What problems did we have again? ‣ No sharing of NSManagedObjects between threads.
  • 63. What problems did we have again? ‣ No sharing of NSManagedObjects between threads. ‣ Context locking
  • 64. Sharing: solution ‣ Pass only NSManagedObjectIDs to other threads, not objects. ‣ Refetch the object on a different thread or queue to work with it. ‣ Don’t forget to save the ‘original’ object first before working with it on the second thread or queue.
  • 65. Complex queries ‣ Use the same technique for complex or large queries. 1. do the heavy lifting in the background 2. pass list of NSManagedObjectIDs to another thread (e.g. UI thread). 3. load objects as faults, and let Core Data fault them in when you need them (e.g. when accessing a property) ‣ That’s lot of requests, but this is actually more performant and desirable in most cases.
  • 66. Locking: solution ‣ Use child contexts with their own dispatch queues. ‣ Use: performBlock: and performBlockAndWait:  carefully. ‣ Deadlocks still possible, especially with performBlockAndWait:
  • 67. A word about NSManagedObjectIDs ‣ Two types of IDs: ‣ temporary ‣ when the object hasn’t been persisted to a store ‣ permanent ‣ when the object has been persisted to a store
  • 68. A word about NSManagedObjectIDs ‣ Subtle bug: temporary IDs from a non-root context don’t get updated to permanent IDs when saving in the root context ‣ The object is saved just fine, but the ID is not updated correctly. ‣ When passing these around to other contexts after saving: you won’t find the object in another child context!
  • 69. A word about NSManagedObjectIDs ‣ To the rescue: -­‐  (BOOL)obtainPermanentIDsForObjects: (NSArray  *)objects  error:(NSError   **)error;
  • 70. A word about NSManagedObjectIDs -­‐  (BOOL)obtainPermanentIDsForObjects:(NSArray   *)objects  error:(NSError  **)error; ‣ Transforms objects with temporary IDs to permanent IDs (through the persistent store of the root context). ‣ Do this when creating a managed object and you’re safe. ‣ Obtaining permanentIDs is batched, so the performance hit is not that high
  • 71. MagicalRecord ‣ I still use MagicalRecord: ‣ Reduced form: no more “automatic” context handling --> dangerous! ‣ Added some extra sauce to work with the nested contexts. ‣ The methods MR supplies still allow for a speedup when coding.
  • 72. Useful References ‣ Nested context release notes: http://developer.apple.com/library/mac/#releasenotes/ DataManagement/RN-CoreData/_index.html ‣ Magical Record: https://github.com/magicalpanda/MagicalRecord ‣ Mogenerator: http://rentzsch.github.com/mogenerator/ ‣ A good read on Cocoanetics: http://www.cocoanetics.com/2012/07/multi-context-coredata/ ‣ Core data programming guide: http://developer.apple.com/library/mac/#documentation/ cocoa/Conceptual/CoreData/cdProgrammingGuide.html ‣ Butane: http://getbutane.com
  • 73. Thanks for listening. Questions? Contact me: Twitter: @inferis App.Net: @inferis E-mail: [email protected] vCard: http://inferis.org