Objective-C 中“oneway void”的用例?

发布于 2024-10-27 23:20:20 字数 165 浏览 5 评论 0原文

发现了 NSObject.h 中一个奇怪的关键字

- (oneway void)release;

我在网上

,了解到它与异步消息传递有关,看起来与 Erlang 的消息传递类似。看来这可以做出很多有趣的事情。该关键字有哪些好的用例?

I found a strange keyword in NSObject.h

- (oneway void)release;

I searched the web, and learned it relates to asynchronous message passing, which looks similar with Erlang's message passing.

It seems this can make many interesting things. What are some good use-cases of this keyword?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满栀 2024-11-03 23:20:20

oneway 与分布式对象 API 一起使用,它允许在不同线程或应用程序之间使用 Objective-C 对象。它告诉系统在方法返回之前不应阻塞调用线程。如果没有它,即使方法的返回类型为 void,调用者也会阻塞。显然,它永远不会与除 void 之外的任何内容一起使用,因为这样做意味着该方法会返回一些内容,但调用者不会得到它。

有关分布式对象的更多信息,请参阅 Cocoa 概念 DistrObjects

oneway is used with the distributed objects API, which allows use of objective-c objects between different threads or applications. It tells the system that it should not block the calling thread until the method returns. Without it, the caller will block, even though the method's return type is void. Obviously, it is never used with anything other than void, as doing so would mean the method returns something, but the caller doesn't get it.

For more on distributed objects, see Cocoa Conceptual DistrObjects.

Bonjour°[大白 2024-11-03 23:20:20

根据Apple的文档,oneway仅用于分布式对象(而不用于多线程)。

仅当对象是远程对象时才使用 oneway 修饰符。在这种情况下,释放调用可以异步返回(在方法终止之前)。在网络中这是有意义的,因为等待返回消息可能需要一段时间。

release方法没有返回值,因此调用它可以异步执行。相反,retain 和 autorelease 返回一个 id,因此我们必须等待返回消息在整个网络中传输。

According to Apple's documentation oneway is only used for distributed object (and not for multithreading).

The oneway modifier is only used if the object is remote. In this case the release call can return asynchronously (before the method has terminated). In a network it makes sense because waiting for a return message can take a while.

The release method has no return value and so call it can be executed asynchronously. In contrast, retain and autorelease return an id and so we have to wait for the return message to be transferred throughout the network.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文