Key Points of Block usage

Source: Internet
Author: User

Key Points of Block usage
Introduction to Block

The Block actually contains two parts.

 

Block location in memory

There are three types of Block in memory: NSGlobalBlock, NSStackBlock, and NSMallocBlock.

  • NSGlobalBlock: similar to a function, in the text segment;
  • NSStackBlock: in the stack memory, the Block will be invalid after the function returns;
  • NSMallocBlock: Located in heap memory.
When a Block is used by another Block, when another Block is copied to the stack, the Block used will also be copied. However, the Block as the parameter does not copy. Basic Types of Block access to different types of Variables

Applicable to MRC and ARC scenarios

  • Local Automatic variables are read-only in the Block. When a Block is defined, the value of the copy variable is used as a constant in the Block. Therefore, even if the value of the variable changes outside the Block, its value in the Block is not affected.
  • Static and global variables. Block to read and write them. Because the addresses of global or static variables in the memory are fixed, the Block reads the variable value directly from its memory and obtains the latest value, instead of the copy constant during definition.
  • Block Variable__blockThe modified variable is called a Block variable. Block variables of the basic type are equivalent to global variables or static variables.
ObjC object

Different from the basic type, Block will change the reference count of the object.

MRC:

  • Partial automatic variables: When Block copy is used, the system automatically retests the object to increase its reference count.
  • Static variables and global variables are fixed in the memory, so no retain object will be retained during Block copy.
  • Block variable, which does not retain during Block copy.
  • Instance variable. When Block copy is used, the instance variable object itself is not directly retained, but the owner self of the instance variable is retained. So you can directly read and write instance variables in the Block.

ARC:

  • Only when the local variable is used, the block copies the pointer and strongly references the object pointed to by the pointer once. For other variables such as global variables and static variables, the block does not copy the pointer and only strongly references the object pointed to by the pointer once.
  • _ Block Variable, during Block copyRetain _ block Variable
    • struct __Block_byref_intValue_0{    void *__isa;    __Block_byref_intValue_0 *__forwarding;    int __flags;    int __size;    int intValue;};struct __main_block_impl_0{    struct __block_impl impl;    struct __main_block_desc_0* Desc;    __Block_byref_intValue_0 *intValue; // by ref    __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_intValue_0 *_intValue, int flags=0) : intValue(_intValue->__forwarding)    {        impl.isa = &_NSConcreteStackBlock;        impl.Flags = flags;        impl.FuncPtr = fp;        Desc = desc;    }};

       

  • The local variable marked as _ weak or _ unsafe_unretained. The block will still strongly reference the pointer object once.

Reference: http://my.oschina.net/u/1432769/blog/390401? Fromerr = QO9ABGMC

Http://tanqisen.github.io/blog/2013/04/19/gcd-block-cycle-retain/

Related Article

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.