在Cocoa中的进程之间传递数据的最佳实践 [英] Best practices for passing data between processes in Cocoa

查看:148
本文介绍了在Cocoa中的进程之间传递数据的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决一个问题,这需要我在我的64位Cocoa应用程序中执行以下操作:

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


  1. 一个32位的Cocoa helper工具(命令行工具)。此帮助程序将打开一个文件(精确的快速时间电影),并使用32位的API(Quicktime-C API)访问该文件的信息

  2. 位进程需要传递回64位应用程序。

  3. 64位应用程式应等到32位元程序完成后再继续

在Cocoa中有许多方法可以实现这一点,但是从我收集的这些是我可以采取的两种方法。

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


  1. 使用NSTask生成32位进程

  2. 将NSTasks stdoutput重定向到管道,并从该管道读取数据

  3. 解析管道中的数据,这将涉及将stdout中的字符串转换为数据(int,浮动,字符串等)。



选项2:使用NSDistributedNotificationCenter的NSTask



Option 2: NSTask with NSDistributedNotificationCenter


  1. 使用NSTask生成32位进程

  2. 当数据在32位进程中就绪时,向分布式通知中心发送NSNotification,并在包含所有相关数据的事件中包含字典。 / li>
  3. 在64位应用程序中订阅相同的NSNotification

所以我的问题StackOverflowers'是,哪个选项是更好的?

这是更好的做法吗?

哪个更有效?

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

我倾向于选项2,因为似乎会涉及更少的代码。如果这两种方法不是很好,有没有更好的方法来做到这一点?

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?

推荐答案

一个应用程序。不要使用NSTask,它会混淆启动服务。 (如果你的意思是它是一个帮助工具,好奇的专家用户可以从命令行运行它,然后NSTask是OK。)

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.)

DNC将工作,但如果子进程确实是一个应用程序,不要使用NSTask + NSPipe - 使用分布式对象。

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

这篇关于在Cocoa中的进程之间传递数据的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆