Ios:arc and non-arc using the Block property problem

Source: Internet
Author: User

1. Block declarations and thread safety

The declaration of the block attribute first needs to use the copy modifier, because only the copy 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 are not sure (usually this is the case), then you first need to declare that the Block property is atomic, that is, to guarantee the atomicity of the variable (OBJECTIVE-C does not enforce the atomic nature of the pointer read and write, C #).

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 note that the Block is to be release under non-arc.

However, with atomic to ensure that basic atomicity is still not thread-safe, then the block must be assigned to local variables at the time of invocation 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, Self.myblock may be changed to null by another thread, causing crash    //Note: Atomic will only ensure 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 local immutable if (block) {    block (123);}

In the non-arc you need to manually retain, otherwise if the property is empty, the local variable is a wild pointer, the following code:

Non-Arcmyblocktype block = [Self.myblock retain];if (block) {    block (123);} [Block release];

Back to Catalog

2. Circular reference issues

Circular references are another common problem when using block.

Under Arc, because the __block 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 __weak, before the use of only __unsafe_unretained, __ The disadvantage of unsafe_unretained is that the pointer does not empty itself after it is released. Example code:

IOS 5 can be used before __unsafe_unretained//__unsafe_unretained typeof (self) weakself = Self;__weak typeof (self) weakself = self; Self.myblock = ^ (int paramint) {    //use weakself to access the self Member    [weakself Anotherfunc];};

In the non-arc, obviously cannot use weak reference, here can directly use __block to modify the variable, it will not be retain by block, reference code:

Non-Arc__block typeof (self) weakself = Self;self.myblock = ^ (int paramint) {    //use weakself to access the self Member    [weakself Anotherfunc];};

Ios:arc and non-arc using the Block property problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.