Cocoa – Best practices for passing data between processes in Cocoa

cocoaevent handlingeventsipcnsnotifications

I am in the middle of solving a problem which requires me to do the following in my 64-bit Cocoa application:

  1. Spawn a 32-bit Cocoa helper tool (command line tool) from within my application. This helper will open a file (a quicktime movie to be precise) and access information about that file using 32-bit only APIs (Quicktime-C APIs)
  2. The data gathered from the 32-bit process needs to be passed back to the 64-bit application.
  3. The 64-bit app should wait until the 32-bit process completes before continuing

There are many ways to accomplish this in Cocoa, but from what I gather these are two approaches I could take.

Option 1: NSTask with Pipes

  1. Use NSTask to spawn the 32-bit process
  2. Redirect the NSTasks stdoutput to a pipe, and read data from that pipe in the 64-bit process.
  3. Parse the data from the pipe, which will involve converting strings from stdout into data (ints, floats, strings, etc.)

Option 2: NSTask with NSDistributedNotificationCenter

  1. Use NSTask to spawn the 32-bit process
  2. When data is ready in the 32-bit process, send an NSNotification to the Distributed notification center, and inlude a dictionary in the event with all of the pertinent data.
  3. In the 64-bit app subscribe to the same NSNotification

So my question for StackOverflowers' is, which option is "better"?
Which is a better practice?
Which is more efficient?

I'm leaning towards Option 2 because is seems like there will be less code involved. If these two approaches aren't great, is there a better way to do this?

Best Answer

You say that the subprocess will be an application. Don't use NSTask for that—it confuses Launch Services. (If you mean it's a helper tool, such that a curious expert user could run it from the command line, then NSTask is OK.)

The DNC will work either way, but if the subprocess really is an application, don't use NSTask+NSPipe—use distributed objects.

Related Topic