Block in Layman's

Source: Internet
Author: User
Tags gcd

    • Research tools
      • Clang in order to study the compiler implementation principle, we need to use the clang command. The clang command can be used to rewrite the source code of the objetive-c into C + + language, which can be used to study the source code implementation of each feature in block.
      • CLANG-REWRITE-OBJC main.m
      • The Uikit framework cannot be included in main.m, and parsing in the command line is not recognized. Includes #import <Foundation/Foundation.h> is supported
    • What kinds of variables are in C language
      • Automatic variables
      • function parameters
      • static variables
      • Static global variables
      • Global variables
    • The characteristics and principle of each variable type in block
      • Automatic variables
        • Cannot be modified, carry __block decoration can be modified
        • Will be held by block (retaincount+1)
        • will be copied into block without __block.
      • function parameters
        • can be directly modified
        • Will not be held by block (Retaincount will not increase)
      • static variables
        • can be modified-because the memory address value is passed to the block, view the specific implementation of the block (see the main.cpp file after clang)
      • Static global variables and global variables
        • can be accessed and modified directly-due to the storage area in the zone global Zone, due to the reason of the area of action
        • Will not be held by block (Retaincount will not increase)
    • How variable values are changed in a block
      • Pass memory address to block
        • The memory pointed to by the pointer is not modifiable, but the data stored in the memory can be modified
        • The nsmutablestring variable can be appendstring directly in the block body, but cannot be
      • Using __block retouching
        • Block converts the variable that this identifier modifies into a struct, which is passed in the block body and is used by the struct
        • __block int I will be converted into
        • struct __block_byref_i_0 {  void// point to itself // point to itself when copied to heap (heap), Original block This field points to the block address on the heap, and this field on the pair still points to itself. This way, regardless of how __block is copied to the heap, or on the stack, you can access the variable value through (i->__forwarding->i). int int int i; 
          };
      • Block captures external variables only captures the values that are used in block closures, and other values that are not used, and are not captured. And the variables that block can capture are only automatic variables and static variables.
    • Type of block
      • _nsconcretestackblock
        • Only external local variables, member property variables, and blocks that do not have a strong pointer reference are stackblock.
        • The life cycle of the stackblock is controlled by the system, and once returned, it is destroyed by the system.
        • Do not hold objects
      • _nsconcretemallocblock
        • A block that is referenced by a member property with a strong pointer reference or copy decoration is copied to the heap as a mallocblock
        • Destruction with no strong pointer reference, life cycle controlled by programmer
        • Holding objects
      • _nsconcreteglobalblock
        • No external variables or blocks that use only global variables and static variables are _nsconcreteglobalblock
        • Life cycle from creation to application end
        • Do not hold objects
      • ARC, the system will determine if the block is copied to the heap according to the following rules
    • System call copy for block replication
      • Call copy manually (when block is a function parameter, we need to manually copy one copy to the heap.) This removes the system API we do not need to tube, such as GCD and other methods of the method itself with Usingblock)
      • Block is the return value of the function
      • Block is strongly referenced (block is assigned to __strong or ID type)
      • Calling the system API into the parameter contains Usingblcok method
    • __block Stack Copy
      • MRC only objects that have copy,__block modifiers will be copied to the heap.
      • ARC has a copy or = (The block type is passed by =, which causes the call to the Objc_retainblock->_block_copy->_block_copy_internal method chain), __ Block-modified objects are copied to the heap
      • __block modified objects will be copied to the heap: __nsstackblock__ type block converted to __nsmallocblock__ type
    • Clang Code Conversion
      • MAIN.M file 30 lines, size 831 bytes. Converted Main.cpp file 104810 lines, size 3.1MB.
    • Block Circular Reference
      • The conditions that cause circular references are actually very harsh:
        • Blocks need to be strong or copy-like operations by related classes (the current class or a nested reference)
        • Use self in block body (including member variables, member properties, etc.)
      • How the disassembly of a circular reference occurs:
        • Using __weak to weak references to self is actually a weak reference to unlock the closed loop
          • __weak __typeof (self) wself == ^{    = wself;     // use self for related actions };
        • Pass the self as a parameter to the block using the formal parameter
      • Common and confusing scenarios (if the block is not strong or copy, the first of the harsh conditions)
        • GCD, System animation and other systems block Api,block direct use of self in the body is not a problem
        • Member attributes or member variables are used in block body, no problem (refer to block type)
        • Access to static variables, global variables, global static variables, does not cause problems

Block in Layman's

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.