Callbacks from async blocks
Callbacks from async blocks
- Subject: Callbacks from async blocks
- From: Oleg Krupnov <email@hidden>
- Date: Sat, 16 Mar 2013 13:43:06 +0200
I want to switch to blocks instead of delegates for small callbacks,
e.g. operationDidFinish or animationDidFinish, but there is a question
I cannot understand.
There is the typical scenario when I need to run an operation (usually
in another thread) and while it is running, I should ignore new
requests to start another such operation. Also, I need to accept
delegate calls (back into the main thread) only from the current
operation, i.e. if I have already ditched an operation that was still
running, I should ignore its delegate call when it completes.
With delegates, I simply save the current operation in a caller's
property when the operation starts, and reset it in delegate method
operationDidFinish:. So when the new request is coming, I check if the
current operation is nil, and only then create a new operation. In the
operationDidFinish: method, i check if the current operation is equal
to the sender, and only then respond with an action.
However, when I use completion blocks instead of delegates, things get
somewhat vague.
Is it still necessary to store the current operation in a property, or
does each operation get its own independent copy of the completion
block, together with its current context? What happens if I create
another operation and launch it with its completion block - will the
first operation still fire the block when it's done? If so, how do I
correctly discard such call from a ditched operation? I tried to save
the current operation in a property, but then in the completion block,
I don't have anything to compare it with, as the completion block
doesn't provide a sender. Or should I always use blocks with sender as
a parameter?
This looks like a common problem, what is the standard solution?
P.S. It seems that I don't quite understand the nature of blocks. On
one hand, they are objects, i.e. instances of some class, but on the
other hand they appear like pieces of code, so they are classes. When
I write something like operation.completionBlock = ^(){ // some code};
do I create another instance of a block object or am I reusing the
same instance over and over again? Is such writing equivalent to a
object literal, like @"string" for strings? What is even more unclear,
what happens to the context of blocks, i.e. self and ivars of the
object where the block is declared, will that be the same retained
object in all operation instances' completion blocks? How about local
vars declared outside and used inside of the block - will they be
different or the same instances for each operation?
Thanks.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden