How to execute all methods of an object in a certain queue in ios -
for ios app using main queue , root queue. have several objects , want mehtods run in root queue. far, have been doing add dispatch_async each time call 1 of methods become troublesome when use more queues , want go main queue. looking way assign objects root queue methods executed in roof queue. mean looking sth. this: [[testclass alloc] initinqueue:testqueue];
it possible create in manner similar kvo. swizzle methods wrap them dispatch_*
calls, discourage it. level of magic high, , tie in knots. moreover, can't wrap arbitrary method in dispatch_async
since can't have return result that. can't wrap arbitrary methods in dispatch_sync
because deadlock. problems of solving general case spiral out of control in opinion.
what should asking instead whether queue architecture correct. need keep calling many small methods on other queues? in many cases better encapsulate full work units (i.e. coherent sequence of operations take input , generate final result) rather individual method calls. (once think in work units, nsoperation
gets lot more useful.) while useful wrap accessor queue thread-safety, not general solution concurrency.
while there advantages getting off of main queue, advice shouldn't over-applied. can reasonable amounts of work on main queue without problems. built single-threaded cocoa apps on computers less powerful iphones long before gcd. (the iphone more powerful old powerbooks , might more powerful original macbook.) i'm not discouraging queues here, make sure you're doing right reasons , don't overcomplicate things.
but if need move work, recommend being explicit dispatch_
calls in method itself. it's little more typing, it's clearer , less error-prone.
Comments
Post a Comment