Objective C Blocks as Async-callbacks & BAD ACCESS?

Well, first off: If (and only if) your reason for avoiding the use of self or direct access of ivars inside of a block really are retain-cycles, then you should be in a situation like.

Well, first off: If (and only if) your reason for avoiding the use of self or direct access of ivars inside of a block really are retain-cycles, then you should be in a situation like client => objectA => blockWithWeakBackReference (where => means 'has a strong reference to'). In this case, blockWithWeakBackReference should only ever be invoked by objectA, so there is no danger of a BAD ACCESS. If I understand your question correctly, what you really mean is a different scenario: objectA wants some application-wide service to execute a block on its behalf, if some precondition is met.

You avoid using self inside of the block because you want to be able to dispose of objectA before the block is executed. One example for this might be a shared network-queue that executes a block when the request finished loading for one reason or another. In that case, I would suggest to simply copy the design of NSNotificationCenter's addObserverForName:object:queue:usingBlock: and make your service implement a pair of methods like -(SomeTokenObjectType)addWorkerBlock:(void(^)(whatever-signature-makes-sense-for-you)) and -(void)cancelWorkerBlockWithToken:(SomeTokenObjectType) in order to enqueue and cancel your callback-blocks.

Then, all objects that use this service can simply have an ivar of type NSMutableSet to store the token for every enqueued block and — in their dealloc — enumerate the remaining tokens, canceling them with the service.

That's exactly the scenario i'm dealing with Dany. Thanks a lot for your comments, I really appreciate it. I'm gonna implement that mechanism, I really like the way it looks!.

– Jorge Leandro Perez Aug 2 at 0:01 It's been a while, but I wanted to add a maybe interesting bit of information for anyone stumbling over this to tackle related problems: In his formidable article Let's build NSNotificationCenter, Mike Ash implements this mechanism in a very clean and elegant way—so be a great artist and steal his approach ;-) – danyowdee Nov 26 at 21:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions