Asynchronous message passing in Objective-C
vallettaventures.com
vallettaventures.com
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[queue addOperation:[NSBlockOperation blockOperationWithBlock:^{
// do something
}]];
or NSOperationQueue *queue = [NSOperationQueue mainQueue];
[queue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(selectorName:) object:param1] autorelease]];
Edit: on re-reading the post in detail, I see the problem he is solving is different from the one the above code solves. Clearly there is value to what he has made. I wonder if it could not be re-coded using gcd and/or blocks, but NSOperationQueue is too high-level an API to be appropriate.Still, I can't tell you how many times I've used the code above to create simple async systems.
While you might express that slightly differently if you used GCD or operation queues, you should be able to make it just as readable. Blocks are pretty cheap, so any performance hit should be more than offset by the gains you get from having the OS manage your thread pool for you.
In Objective-C, method calls are dynamically bound. So you can make the following call:
[myObject crazyMethodName:x:y:z]
In Java that'd be myObject.crazyMethodName(x,y,z)
However unlike Java, there doesn't have to be a method called crazyMethodName, and the compiler will still compile it just fine. What happens is that at runtime, when this method is called, the message "crazyMethodName" is sent to myObject. If it doesn't have a method which responds to that name, then Objective-C calls the "forward" message on myObject, passing in the "crazyMethodName" method name and the arguments. "forward" can do what it likes with this -- the default implementation of "forward" is (was?) to issue a No Such Method Found error.
What Distributed Objects did was create a special proxy object which has a single method implemented: "forward". The system worked as follows. When machine A connects to machine B, machine B gives machine A its "entry" object (as defined by the user). B thinks it gave A an object, but in fact what A received was a proxy object. Let's say that the entry object responds to a method "foo". A calls
[obj foo]
obj is actually a proxy which responds to no methods at all, except for "forward". Objective-C fails to call "foo" on obj, so it tries "forward", and lo and behold the proxy has implemented that in a special way -- it wraps up the method call and ships it over to machine B and calls "foo" on the real object.
Through this procedure objects get passed as arguments back and forth through these method calls, which in turn wind up generating more and more proxies. Ultimately neither machine knows that the objects it's got access to are fakes. It just feels like you've been given access to call and manipulate another processes objects. Very clean and elegant.
DO still works in OS X. Frankly, it's a neat hack, but it's prohibitively difficult to build anything robust on top of it:
- Error handling is difficult -- what happens when the other side goes down in the middle of your method call? If you have to add extra exception and timeout handling everywhere, that dwarfs the small convenience of making the dispatch code easy.
- Hard to make it secure.
- It's impossible to interoperate with anything else -- what happens when you want to talk to your DO-based server using Java or Windows or Linux?
See "A Note On Distributed Computing" from 1994 for more caveats on this whole approach:
[1]: http://segment7.net/projects/ruby/drb/introduction.html [2]: http://www.unlimitednovelty.com/2011/05/introducing-celluloi...
The public domain is where things go when their copyright expires. In most countries -- including the United States -- there is no legal procedure to dedicate something to the public domain. You can claim something you wrote is in the public domain until you're blue in the face, but it's not. You still own the copyright on it.
The closest you can get to a public domain declaration is a bare license: basically a license which says "Copyright 2012 by me: I give you the license to do whatever you want with this." The problem with a bare license is that if your code doesn't work right, people can (and do) turn around and sue your pants off. Because -- remember -- the work is not in the public domain. You own it and are responsible for it.
Thus we get to a BSD-style ("academic") license. At a minimum this license says "Copyright 2012 by me: I give you a license to do whatever you want with this as long as you agree not to sue me if something doesn't work".
Always use, at a minimum, a BSD-style license. Public domain is a fiction.
Otherwise, I agree, there are a fair number of inaccuracies in the GP [SeanLuke] comment.
(I ask because I'm curious, not to further a debate about PD).
But I'm particularly interested in Bernstein's ad hominem broadside on Rosen for stating, while general counsel for OSI I believe, that public domain dedication is unlikely in the US. Rosen is "only a lawyer" according to Bernstein -- who I might add is "only a mathematician". Though you have to give him props for representing himself in Bernstein v. Commerce.
I myself think that Rosen's conservative position is the right one. Even Lessig's CC0 dedication is couched in tons of careful "we're not sure if this works" verbiage. At any rate, presuming this is unsettled law -- and it seems to me that it's pretty unsettled -- that alone makes it unwise to be attempting a public domain dedication.
I'm really not interested in the conflict between the OSI and Dan Bernstein. I'm just responding to your comment.
Pretty sure you're wrong about PD being a "fiction".
Well, you did directly cite an article, about a third of which is devoted to smacking down someone with more cred than himself.
That being said, my comment about PD being a "fiction" was overly excessive bomb-throwing, I admit.
But, can I just add two things: that's about as specific a legal argument as I've ever seen an engineer (or mathematician) make, including chapter/verse citations to actual court decisions. Not only are you mischaracterizing it, but you might also take a moment to be specific about which parts of it you disagree with. Why should we be interested in how we feel about people's interpersonal conflicts? Bernstein is right or he isn't.
Second: I didn't call you out for bomb-throwing (that would be hypocritical in the extreme). Can I ask, how much of your original comment do you still stand by?
And, in many cases, everything. SQLite users have had this issue in the past: http://www.sqlite.org/copyright.html
Which are generally MIT/BSD variants.