112

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?

Christophe
  • 68,716
  • 7
  • 72
  • 138
eonil
  • 83,476
  • 81
  • 317
  • 516
  • 6
    Easy to remember: `oneway void` is one of those beautiful accidental metaphors we sometimes see in coding, in this case for death. "If there's anything on the other side, don't tell us." – buildsucceeded Feb 13 '13 at 10:21

2 Answers2

114

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.

Josh
  • 2,324
  • 1
  • 21
  • 29
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • 6
    The link doesn't seem to work anymore. I couldn't find it within Apple's docs but I found: http://chachatelier.fr/programmation/fichiers/cpp-objc-en.pdf which provides a good explanation of the "oneway" keyword. See section 4.4.5 (pdf page 24) – jlmendezbonini Aug 26 '11 at 18:48
  • @Robin Thanks for the update. It was gone for the past few months, but I couldn't find another reference from Apple so I just left it. – ughoavgfhw Nov 05 '11 at 20:00
  • and I thought the only way to fire network tasks is to sub-thread them...using gcd_async and its friends.. – Nirav Bhatt May 17 '16 at 13:34
21

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.

Freeman
  • 5,810
  • 3
  • 47
  • 48
  • I have a related question: If using the `oneway` keyword causes the message not to block the calling thread while the called method is executed, that makes the called method to be executed on a second thread. Isn't that multitasking (in its simplest form)? Can I use this to detach some *small* operations quickly (without much effort) to another thread? Thank you very much. – Constantino Tsarouhas Jul 04 '11 at 20:31
  • 2
    No, you can't. In distribute computing the thread is executed in a different machine/process. You can use performSelector: onThread: withObject: waitUntilDone: for your purpose. – Freeman Jul 13 '11 at 10:43
  • 2
    So what happens with `- (oneway void) release` then? It's not on another machine or process. – Constantino Tsarouhas Jul 13 '11 at 22:31
  • +1 for explaining what exactly one way is about precisely -->defining `one way` to a method means `don't wait for the method to complete and return anything. block it if there is any n/w trafic or so.` Do I make sense? – geekay Apr 26 '12 at 08:02