iOS/Objective-C: Use "oneway" keyword for asynchronous call on methods and usage of in, out, byref, bycopy keywords
Just encountered this "oneway" keyword in iOS when I was trying to implement a Singleton Pattern.
So here is the example,
The use of "oneway" keyword above simply tells that, it is asynchronous (the result is not immediately expected) - hence it must return void. It 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 clarification on the second method above, keywords "bycopy" and "out" simply means that,
bycopy - parameter is transmitted by copy;
and
out - parameter is an output variable.
There are other keywords also that allow for more efficient communication between distant objects. These are, "in", "inout", "byref".
in - parameter is an input variable
inout - parameter can be used in both ways (input and output) but for optimization purposes, it's better to use in/out or be concise of your parameter on what it must have to achieve and it's used.
byref - parameter is transmitted by reference (without copy) ;
By default, parameters are considered to be inout, except const pointers, that are supposed
to be in. Choosing in or out instead of inout is an optimization. The default mode to transmit
the parameters is byref, and the methods are synchronous by default (without oneway).
For parameters transmitted by value, like non-pointers variables, out and inout make no
sense, only in is correct.
So here is the example,
- (oneway void)release;
-(oneway void) giveMeAnObjectWhenAvailable:(bycopy out id *)anObject;
The use of "oneway" keyword above simply tells that, it is asynchronous (the result is not immediately expected) - hence it must return void. It 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 clarification on the second method above, keywords "bycopy" and "out" simply means that,
bycopy - parameter is transmitted by copy;
and
out - parameter is an output variable.
There are other keywords also that allow for more efficient communication between distant objects. These are, "in", "inout", "byref".
in - parameter is an input variable
inout - parameter can be used in both ways (input and output) but for optimization purposes, it's better to use in/out or be concise of your parameter on what it must have to achieve and it's used.
byref - parameter is transmitted by reference (without copy) ;
By default, parameters are considered to be inout, except const pointers, that are supposed
to be in. Choosing in or out instead of inout is an optimization. The default mode to transmit
the parameters is byref, and the methods are synchronous by default (without oneway).
For parameters transmitted by value, like non-pointers variables, out and inout make no
sense, only in is correct.
Comments
Post a Comment