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?

1
  • 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." Commented Feb 13, 2013 at 10:21

2 Answers 2

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.

3
  • 6
    The link doesn't seem to work anymore. I couldn't find it within Apple's docs but I found: 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)
    – user546719
    Commented Aug 26, 2011 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
    Commented Nov 5, 2011 at 20:00
  • and I thought the only way to fire network tasks is to sub-thread them...using gcd_async and its friends.. Commented May 17, 2016 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.

4
  • 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. Commented Jul 4, 2011 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
    Commented Jul 13, 2011 at 10:43
  • 2
    So what happens with - (oneway void) release then? It's not on another machine or process. Commented Jul 13, 2011 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
    Commented Apr 26, 2012 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.