Best practices for passing data between processes in Cocoa

我只是一个虾纸丫 提交于 2019-12-05 01:23:25

问题


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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/504122/best-practices-for-passing-data-between-processes-in-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!