目标 C 中“单向无效”的用例? ?

我在 NSObject.h 里找到一个奇怪的关键词

- (oneway void)release;

我在网上搜索了一下,发现它与异步消息传递有关,与 Erlang 的消息传递类似。

这似乎可以使许多有趣的事情。什么是一些很好的用例这个关键字?

18310 次浏览

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.

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.