Let's get to the block.

Source: Internet
Author: User

Http://www.jianshu.com/p/e03292674e60

Before I start, I'd like to ask you a few questions to see if you have any doubts about this. Tang Qi has written an article about block, so you can take a look at it (this article will quote some of the diagrams and codes that appear in the Gothic text). On the basis of skillful brother, I add some knowledge points and codes related to block, and summarize and revise some viewpoints.

What is 1.block? is block an object?

What are the 2.block types? __blcok the role of the keyword?

What is the difference between the 3.block under Arc and MRC?

4.block life cycle?

5.block is not a strong reference to an object passed in as a parameter??

What is block? is block an object?

Let's start by describing what a closure is. On Wikipedia, the definition of closures) is:

In programming languages, a closure are a function or reference to a function together with a referencing environment-a tab Le storing a reference to all of the non-local variables (also called free variables or upvalues) of this function.

In translation, a closure is a function (or pointer to a function), plus the external context variable (sometimes called a free variable) that the function executes.

Block is actually the implementation of the Objective-c language for closures.

Block is not an object? The answer is obvious: yes.

is the block data structure definition, obviously, in the block_layout, we see the ISA pointer, here we do not specifically on the ISA pointer expansion, nor block specific data structure to expand, want to know more detailed can see Tang Qi article.

Back to the above, why is the block an object because of the ISA pointer. So what is this ISA pointer?

All objects have the ISA pointer, which is used to implement object-related functions.

See this, you should understand that block is actually OBJC for the closure of the object implementation.


Data structure of block What kinds of blocks are divided into? __blcok the role of the keyword?

Divided into three kinds, namely Nsconcreteglobalblock, Nsconcretestackblock, Nsconcretemallocblock.

Detailed analysis of these three kinds of blocks, the first is nsconcreteglobalblock:

Simply put, if a block clock does not reference an external variable and is not held by another object , it is nsconcreteglobalblock.

As shown in the following:


Nsconcreteglobalblock

It is important to note that Nsconcreteglobalblock is a global block that has been determined during compilation, just like a macro.

What is Nsconcretestackblock:

As you can see, Nsconcretestackblock is a block that references an external variable, on the code:


Nsconcretestackblock

OK, we already know nsconcretestackblock, so what's the difference between it and nsconcreteglobalblock? Is it just a matter of referencing the difference between an external variable or not? The answer is in the negative.

In fact, nsconcretestackblock inside there will be a structural body __main_block_impl_0, this structure to save external variables, so that their volume becomes larger. And this leads to the Nsconcretestackblock not like a macro, but a dynamic object. And it is not held, so inside it, it will not hold its externally referenced object.

The evidence is as follows:


Nsconcretestackblock does not hold external objects

As you can see from the printed log, the reference count remains the same.

Nsconcretemallocblock:

Seemingly the most mysterious Nsconcretemallocblock is actually a block is copied, will generate Nsconcretemallocblock (block no retain). How about, isn't it simple 0 0


Nsconcretemallocblock

It is important to note that Nsconcretemallocblock will hold external objects!


Nsconcretemallocblock will hold external objects

See, as long as this nsconcretemallocblock exists, the reference count for the internal object will be +1.

Here's the key word for __block:

The first example, you'll soon understand.


__block example1

Yes, as mentioned earlier, the block reference outside is captured in the form of capture, and without declaring __block, the external variable is copied into block, and if __block is used, it is accessed by copying its reference address. That is why the __block has been declared, and the change within the block will have an impact on the outside.

Attention!! It is necessary to know that in the MRC environment, if the __block is not used, the external object will be copied, and the use of __block will not use copy operation.

On the code:


__block example2

hahaha, so from a lower point of view, in the MRC environment, __block does not perform a copy operation on the object pointed to by the pointer, but simply copies the pointer. And this is often a lot of novice & veteran do not know!

In the ARC environment, for external objects declared as __block, inside the block will be retain, so that in the block environment can safely reference external objects, so beware of circular reference problems!

What is the difference between blocks in arc and MRC?

First of all, I want to correct the point of view of Qiao Brother blog:

In the case of ARC opening, there will be only blocks of type Nsconcreteglobalblock and Nsconcretemallocblock.

In the above introduction of Nsconcretestackblock, is running in the ARC environment, and printed out the log clearly shows that the block type was nsconcretestackblock.

And in fact, why do people generally think that there is no nsconcretestackblock under arc?

This is because we often assign the block to the variable, and arc under the default assignment operation is strong, to the block body will naturally become copy, so the often printed block is nsconcretemallocblock.

So, under Arc, most of the application scenarios, almost all of them are nsconcretemallocblock or nsconcreteglobalblock. So the problem is, we know that Nsconcretemallocblock is going to hold an external variable, and if it holds an external variable that just holds it, it will have a circular reference problem.

Let's talk about the life cycle of block!

The life cycle of a block?

When it comes to the block life cycle, this is a very serious topic, although the block is easy to use, old and young, but it can easily cause "strong-to-be-blown" ( memory leaks ) when used carelessly.

PS: The next example is the use of Arc to show

First show:


Circular references

No, this object will never be released, which is a typical circular reference case. Object holds block (the reader can imagine why this is nsconcretemallocblock, the hint: in the ARC environment), and block holds the object, causing a deadlock, and object will no longer be released. At this point the witty compiler gives you warning, but in many complicated cases, the compiler does not recognize the loop-referenced scenario. And now you need to pay attention!

So, how do I deal with the life cycle of block-related issues, first of all mentioned that block is an object, and since it is an object, it must have the same life cycle as the object that will be freed if not referenced.

So the life cycle of a block is simple enough to see if the object holding the block is also being held by a block, and if it is not held, there is no need to worry about circular referencing.

But like in the above situation, if produce mutually holds the situation to be swollen to do!

You can use __weak (ARC) or __block (MRC) to solve:


Weak resolving circular references

Look, now it's ready to be released happily.

Does block have strong references to objects that are passed in as arguments?

Alas, unconsciously already almost 2 O ' Night, for this part of the words, in fact, is also idle egg pain in thinking of the problem.

In fact, blocks, like functions and methods, do not hold the parameters passed in

The evidence is as follows:


Block does not hold parameter objects Summarize:

Here, the introduction to block is over. Practical use in fact not too concerned about these principles, only need to correctly grasp the life cycle of the block can be flexibly used block. But for a veteran developer, the deep mastery of block is still necessary!



Wen/kuailejim (author of Jane's book)
Original link: http://www.jianshu.com/p/e03292674e60
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Let's get to the block.

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.