1. Block declarations and thread safety
The declaration of the block attribute first needs to use the copy modifier, because only copy after the block will be in the heap, the life cycle of the block in the stack is bound to the stack, you can refer to the previous article (IOS: Non-arc back block).
Another issue to be aware of is thread safety, which needs to be confirmed when declaring the Block property "is it possible for another thread to modify block when calling block?" "This problem, if it is determined that this does not happen, then the block attribute declaration can be used nonatomic . If you're not sure (usually this is the case), then you first need to declare the Block property to be atomic , that is, to guarantee the atomic nature of the variable (OBJECTIVE-C does not enforce the atomic nature of the pointer reading and writing, C # has).
For example, such a block type:
typedef void (^myblocktype) (int);
Attribute declaration:
@property (copy) Myblocktype Myblock;
Here the arc and non-arc declarations are the same, and of course notice that the Block is under non-arc release .
However, atomic to ensure that the basic atomicity is still not thread-safe, then at the time of invocation, the block must be assigned to local variables to prevent the block from suddenly changing. Because if this is not the case, even if you first judge that the Block property is not empty, before the call, once another thread has set the Block property empty, the program will be crash, the following code:
if (Self.myblock)
{
At this point, the Self.myblock may be changed to empty by another thread, causing crash
Note: Atomic only ensures the atomicity of Myblock, which is inherently non-thread-safe.
Self.myblock (123);
}
So the correct code is (ARC):
Myblocktype block = Self.myblock;
Block is now locally immutable.
if (block)
{
Block (123);
}
In the non-arc requires a manual retain , otherwise if the property is empty, the local variable is a wild pointer, the following code:
Non-arc
Myblocktype block = [Self.myblock retain];
if (block)
{
Block (123);
}
[Block release];
Return to Directory 2. Circular reference issues
Circular references are another common problem when using block.
Under Arc, because __block the grab variable will be block retain , so you must use a weak reference to solve the circular reference problem, IOS 5 can be used directly after the use of the __weak only __unsafe_unretained , the __unsafe_unretained disadvantage is that the pointer released itself will not be empty. Example code:
__unsafe_unretained can be used before IOS 5
__unsafe_unretained typeof (self) weakself = self;
__weak typeof (self) weakself = self;
Self.myblock = ^ (int paramint)
{
Accessing the self member using weakself
[Weakself Anotherfunc];
};
In the non-arc, obviously cannot use weak reference, here can be used directly __block to modify the variable, it will not be retain the block, the reference code:
Non-arc
__block typeof (self) weakself = self;
Self.myblock = ^ (int paramint)
{
Accessing the self member using weakself
[Weakself Anotherfunc];
};
Ios:arc and non-arc using the Block property problem