Best practices for passing data between processes in Cocoa
Asked Answered
W

2

4

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?

Wheelhorse answered 2/2, 2009 at 17:47 Comment(0)
K
3

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.

Kidder answered 2/2, 2009 at 18:5 Comment(2)
Ah, my mistake. I really meant that the 32-bit app will be a helper tool (command line utility). I'll update the problem description to clarify. Thanks for the help, -NickWheelhorse
That's what I thought. In that case, NSTask + NSPipe is OK, but generally better for output that you might otherwise expect to find in a file (e.g., HTML, RTF, image, sound, movie). For smaller or mixed (e.g., HTML + movie) payloads, DNC or DO may be a better bet.Kidder
J
2

NSDistributedNotificationCenter will work okay, but keep in mind your application isn't "guaranteed" to receive distributed notifications by the OS. I haven't actually seen this in practice, but it's something to keep in mind when you're choosing a technology.

The other option you didn't mention is distributed objects. Distributed objects is made exactly for this purpose. It handles either serializing or setting up proxy objects that work between processes or over a network. The documentation is a bit lacking, it doesn't support some newer parts of Cocoa like bindings, it's not exactly easy to use, but I still prefer it when I'm working two processes that work together in a complex way.

Janey answered 5/2, 2009 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.