6 points to consider when using block blocks

Source: Internet
Author: User

For just learning OC New partner, block blocks have been a relatively tangled, more difficult to understand the knowledge point, but after a period of time, will feel very sour. Block blocks of the principle and use I will not repeat, there is a lot of information on the Internet. I have personally used this for a long time, I think the use of block blocks should pay attention to the following points.

First, the block hands need to be sentenced to empty before using block

For example, the block blocks defined by xxxx

if (xxxx) {if (xxxx! = nil) {
XXXX (parameter); or XXXX (parameter);
}                                }

#在使用block块, it is best to empty the block, without the use of empty space processing directly, once the pointer is empty, it will directly cause a crash.

Second, block if you are a property variable, copy the block on the stack to the heap

For example, as an attribute, write @property (nonatomac,copy) XXXXXX; (Note: XXXXXX is block blocks)

#如果不进行copy, if the block on the stack is released, the block attribute variable is empty, and the program may have a direct crash.

Third, after the use of the block to the block pointer to the empty value processing, if it is the MRC compilation environment, you must first release the Block object.

#block作为类对象的成员变量, it is possible for a block person to use a class object to participate in an operation in a block to produce a circular reference.

Assigning a block to null is an important way to remove a circular reference. (You can't just do a null-value operation in Dealloc, so that the resulting circular reference will not be destroyed)
For example: if (_sucblock) {
_sucblock (parameter);
}
MRC: First [_sucblock release]; (previous copy)
_sucblock = nil; After use, the block is assigned a null value, the dereference!!!
}

There is also a modification to the block interface design, the possible variables as parameters to the block, from the design to solve the problem of circular reference.

Four, when using the self or member variables to join the block before the first to change to __weak weak reference, which is actually a problem with the third, is to solve the circular reference problem.

V. In a multithreaded environment (where the weakself in a block is likely to be refactored), you need to turn self into a strong pointer, avoiding the Self object being refactored when running to a critical step.

#第四, fifth together, there's a noun called weak–strong dance.

The following code comes from Afnetworking, a classic that uses Weak–strong dance.

__weak typeof (self) weakself = self;
Afnetworkreachabilitystatusblock callback = ^ (Afnetworkreachabilitystatus status) {
__strong typeof (weakself) strongself = weakself;
Strongself.networkreachabilitystatus = status;
if (Strongself.networkreachabilitystatusblock) {
Strongself.networkreachabilitystatusblock (status);
}
};

First line: __weak __typeof (self) weakself = self;
As mentioned in the previous article fourth, in order to prevent callback internal to self strong reference, weak a bit.
It uses __typeof (self), which involves several points of knowledge:
A. Differences between __typeof, __typeof__ and typeof
No difference.
B. For older LLVM compilers, this sentence compiles an error, so it is popular in early Arc users __typeof (&*self).

The third line: __strong typeof (weakself) strongself = weakself;

According to the previous fifth article to return to Strong, here typeof () is written in weakself, it is not a problem to write self, because TypeOf is the compile-time to determine the variable type, so here write self will not be circular reference.

IV Line, if you do not turn into strongself and use weakself, after a few sentences, it is possible that after the execution of the fourth sentence, the object of self may be deconstructed, and then the subsequent stausblock is not executed, resulting in a logic error.

In the last fifth line, the block is empty before use.

The block callback does not work, and the instance object of the class that called the Block property variable is not already the original object.

#这个问题只能具体情况具体分析了, the program may not be wrong to run, that is, the block callback does not work, some features can not be implemented, breakpoint debugging found at all do not go callback. Before I had a colleague have encountered this problem, another colleague to solve for him one hours also did not solve, I let him check the block blocks to call the class object, sure enough, and the original address is not a, he created a new object, before and after spent 1 minutes to solve (smug).

Other questions continue to be summed up, but also want friends to share their own problems??。

6 points to consider when using block blocks

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.