4 ms·
These days, why not use GCD and blocks for this kind of work?
by phil 15y ago
These days, why not use GCD and blocks for this kind of work?
- kennywinker 15y agoMy thoughts exactly. 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.
- phil 15y agoHow is the problem he is solving different? I guess using an actor chain and messages allows you to repeatedly drop many messages through the same sequence of operations. 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.