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.

Sunday, June 17, 2012

Ruby Multithreading using Subprocesses

Since most implementations of Ruby do not have native (OS-level) multithreading, a quick way to achieve parallelism is to refactor the parallel code into a separate ruby code. Then, in your main ruby code, you create threads that invoke the separate ruby code as a subprocess (by calling Kernel#system or IO::popen).

Of course, this strategy only makes sense for very few cases. In mine, I merely wanted to run the same piece of code on a massive number of files. No communication needed.

For example:

# Define the separate ruby code
PROCESS_FILE_BIN = "/home/me/bin/process_file.rb"

# Get files to process
files = get_list_of_files

# Setup mutex
mutex = Mutex.new

# Run up to num_processes
threads = []
num_processes.times {
  threads << Thread.new {
    loop do
      # Get file
      file = nil
      mutex.synchronize do
        file = files.pop unless files.empty?
      end

      # Break if done
      break if file.nil?

      # Process file
      system(PROCESS_FILE_BIN + " " + file)
    end
  }
}

# Wait for threads to finish
threads.each { |t|
  t.join
}

Sunday, May 6, 2012

NSOutlineView Source List -- issues with auto layout

So in my app, I was experiencing a similar problem described by Chris Miller here. Essentially, using auto layout with NSOutlineView is causing two major alignment issues:

  1. The first problem has to do with the baseline of the text. Unselected, the baseline is higher than it should be. Upon selection, the baseline re-adjusts itself correctly. Chris has a neat video on this issue which is available from his article.
  2. The second problem has to do with the horizontal spacing between the disclosure triangle with the NSImageView. This problem was not described by Chris perhaps because I had my NSOutlineView configured as a source list.

Chris' solution to make the height of the row static did not work for me. What ended up working is:

  1. Add a constraint (called "Pin" in the Xcode menu) to make both the image view and the text field have a constant and equal height. This solves the first issue.
  2. After calling reload: on the view, call display in the next run loop. This solves the second issue. You may want to do this before calling expandItem: so that the view does not flicker when it adjusts the distance between the disclosure triangle and the image view.

Wednesday, February 1, 2012

Mac Printing: Disabling Supplies Levels print jobs in SoC

If you are printing to Lexmark printers and you receive a notice regarding a "Supplies Levels" print job, Lexmark has a support page for that.

Wednesday, November 30, 2011

Upgrading from iPhoto '11 to Aperture 3

Apple's iPhoto has a great feature set -- simple and sufficient for day-to-day needs -- but performance comes to a crawl after you've used it for about a year or two. It almost feels like iPhoto is actually Aperture Lite and its supposed to lead you towards that $80 pay wall after one or two years of trial usage.

Anyway, migrating your iPhoto library to Aperture is no simple feat, especially if you're upgrading because your library has outgrown iPhoto's limits (think 100K photos in 3-5K events). Here's what I've learned from my experience:

About the process:
  1. I chose to import from my iPhoto library by leaving the image files in their current location. Im not sure if doing otherwise will make things faster or slower. Something tells me its the latter.
  2. It could literally take up to one day to finish the entire process.
  3. There are three phases: 1) Import, 2) Saving Aperture library, 3) Processing. The first two needs to be done without shutting down Aperture.
  4. The import stage could take up half a day, the CPU may spike in temperature at times, and disk I/O will be pretty busy; so be prepared. 
  5. The import is not resumable. You cannot interrupt or cancel the import. If you do, you have to start over (i.e. by deleting the Aperture library file before starting the app again). Otherwise, Aperture will just create a duplicate entry in your Aperture library if you try the import again -- duplicate library entries are not detected.
  6. Once the import is done, you are not. Closing Aperture will cause it to save its library. This won't take as long as the import, but it can take a couple of hours.
About speed:
  1. If you didn't close aperture or when you open it again, it will be busy processing all the photos it has just imported. This will likely take the longest time, but you can close Aperture at any time. So if aperture is slow after its done with the import, its probably because its still processing all the images.
  2. Having a speedy HDD (7200rpm or solid-state) will make a huge difference
  3. I've read that turning off face detection and automatic preview generation makes a difference -- made no difference to me
  4. You can monitor the whole progress by opening Aperture's Activity window. Deselect any current selection on the main window's side bar and minimize the main window -- this seems to help responsiveness
It seems odd to me that Apple made little effort to make the transition from iPhoto to Aperture easier or at least enable us to do the migration in parts over a few sessions. Who are their target users if not upgrading iPhoto users?

After 1 Aperture crash, 3 failed attempts, I'm... still at the processing stage.

Friday, November 11, 2011

How Rails 2.x and 3.x handles time zones with mysql

Very good post on how rails handles time zones with mysql

Mode 2: Automatic time zone conversion is enabled
  • This is the default when you create a new Rails 3 application, but not when you create a Rails 2 application.
  • In this mode Rails keeps all Time object in the configured zone, but stores them in MySQL as UTC times (regardless of the time zone you chose).