Thursday, August 23, 2012

Lessons on Core Data

Aside from the standard principles (e.g., one context per thread, do not pass managed objects between threads), in the end, what worked for me in developing a multi-threaded Core Data application was the following guidelines:

  1. Always create/update your managed objects off the main thread
  2. Always get permanent IDs for your managed objects upon creation. If you are creating many (100+) of these managed objects in a loop, create an array of uninitialised objects before you enter the loop so you can obtain permanent IDs for all the empty objects with only one trip to the persistent store. You can always delete the unused uninitialised objects later on.
  3. Always fetch managed objects off the main thread. Specify that you only want the objectIDs to be returned. After which, you can pass an array of the objectIDs to the main thread for it to fetch using a single predicate of "self in %@". You can assert that the returned array has the same count as the objectIDs if you want.
  4. Use MagicalRecord for a house of easier and accessible utility methods, but understand what it means to have nested (related) contexts and background saves.
In particular point #2 above is really important to avoid some quirkiness and bugs (or unexpected behaviour may be a better term) that the current implementation of Core Data has. More details by Whitney Young.

Solving Printing Crashes on OS X Mountain Lion

Kudos to the tip from https://discussions.apple.com/thread/4213152?start=0&tstart=0
  1. Open Terminal.app
  2. cd /Library/Printers/hp/PDEs
  3. rm hpPostScriptPDE.plugin
  4. Done!
This solved my problems printing to my home HP printer and office Lexmark printers.