Probe into the deep use of block in iOS development

Source: Internet
Author: User
Tags anonymous

Block is added after iOS 4.0 and is heavily used in the new iOS API. Block is an anonymous piece of code that can be used as a parameter to another object and get the return value. In essence, block is similar to other ordinary variables, except that the data it stores is a function body. Block is not just a patent for objective-c, but a new feature that can be applied to the language level of C, C + +, and objective-c. By using block, a developer can pass a piece of code as a parameter to a function as a value. At the same time, blocks is also a Objective-c object that can be added to Nsarray or nsdictionary collections like other objects.

Block syntax, which is essentially an anonymous function. is similar to the function pointer
Block encapsulates a piece of code that can be executed at any time
A block can be either a function parameter or a function return value, which itself can take an input parameter or a return value.

Block Basic Use

1> How to define a block variable

Int (^sumblock) (int, int);

void (^myblock) ();

Int (^myblock) (int) = ^ (int num) {return num *multiplier};




2> How to use block encapsulation code


^ (int a, int b) {

return a-b;

};



^() {

NSLog (@ "----------");

};



^ {

NSLog (@ "----------");

};



3> block access outside variables

* Inside the block can access the outside variables

* By default, the outer local variables cannot be modified inside the block

* Add __block keyword to local variable, this local variable can be modified within block

* Add static keyword to local variable, this local variable can be modified within block

4> uses typedef to define block types


typedef int (^myblock) (int, int);

You can then use the Myblock type to define the block variable

Myblock Block;

Myblock B1, B2;



B1 = ^ (int a, int b) {

return a-b;

};



Myblock B3 = ^ (int a, int b) {

return a-b;

};



the details and nature of block use

1.block is actually a pointer to the structure.

2. The compiler will generate the corresponding function for the block's internal code

Block 's memory management

1. By default, block memory is in the stack

* It does not perform any action on the referenced object

2. If you do a copy operation on block, the block memory will be in the heap

* It will do a retain operation on the referenced object

* Non-arc: If the referenced object is decorated with __block, the retain action is not done

* ARC: If the referenced object is decorated with __unsafe_unretained\__weak, the retain action is not done



Here is a personal favorite article on block memory management, interested in the study can be seen: block memory management

Block use attention

1. Block pointers need to be processed before using block.



Direct use without NULL, once the pointer is empty directly produces a crash.




if (!self.isonlynet) {

if (Succblock = null) {//after use block before using block to be processed

Return

}

ID data = [nskeyedunarchiver unarchiveobjectwithfile:[self favoritefile]];

if ([Data Iskindofclass:[nsmutablearray class]]) {

Succblock (Data,yes);

}else{

Succblock (Nil,yes);

}

}





2. The user will change self to __weak before it is added to the block by the self or member variable

3. In a multithreaded environment (where the weakself in the block is likely to be refactored), you need to turn self to a strong pointer to avoid the self object being refactored when you run to a critical step.




__weak __typeof (self) weakself = self;

Afnetworkreachabilitystatusblock callback = ^ (Afnetworkreachabilitystatus status) {

__strong __typeof (weakself) strongself = weakself;

Strongself.networkreachabilitystatus = status;

if (Strongself.networkreachabilitystatusblock) {

Strongself.networkreachabilitystatusblock (status);

}

};



Some questions and answers in block use


It is mainly about how to use external variables and memory management of block itself.

First define a block variable, which is used as a follow-up example:


typedef void (^BLOCKCC) (void);
BLOCKCC _block;


1. Referencing external variables in block


You can use external variables directly in the block, such as


int number = 1;
_block = ^ () {
NSLog (@ "number%d", number);
};


In fact, when block is generated, number is encoded into the block as a constant variable. As you can see, the following code, the number value in the block is not going to change:


int number = 1;
_block = ^ () {
NSLog (@ "number%d", number);
};
Number = 2;
_block ();

The output value is 1 instead of 2. The reason is as stated above.

If you try to change the value of an external variable in the block, you will get an error. The solution to this problem is to introduce the __block identifier. Identify variables that need to be modified within the block as __block scope. The changed code is as follows:


__block int number = 1;
_block = ^ () {
number++;
NSLog (@ "number%d", number);
};

And this time, in fact, the block outside the number and block internal number points to the same value, back to just the external change block example, its output will be 2, rather than 1. Interested can write an example of their own try.

2, Block's own memory management


Block itself is like an object that can be retain, and release. However, when the block is created, its memory is allocated on the stack (stack), not on the heap (heap). His own domain is part of the scope of the creation, once the scope of the action outside the creation of the call block will cause the program to crash. Like the following example.
I created a block in view did load:


-(void) viewdidload
{
[Superviewdidload];

int number = 1;
_block = ^ () {

NSLog (@ "number%d", number);
};
}

And this block is called in the event of a button:


-(Ibaction) Testdidclick: (ID) Sender {
_block ();
}

When I press the button it causes the program to crash, and the way to solve this problem is to invoke the Copy method when the block is created. Copy will move the block from the stack to the heap, so it can be used elsewhere.
Modify the code as follows:


_block = ^ () {
NSLog (@ "number%d", number);
};

_block = [_blockcopy];

Similarly, it is important to note that when the block is placed in the collection class, if the generated block into the collection class, you can not use block elsewhere, you must copy the block. But the code looks relatively strange:


[Array addobject:[[^{
NSLog (@ "hello!");
} Copy] autorelease]];


3. Circular reference


This is actually a small derivative in the 1th. When using member variables inside the block, such as
 

    @interface viewcontroller:uiviewcontroller
     {
        nsstring *_string;
   }
    @end

in block creation:
 

    _block = ^ () {
         NSLog (@ "string%@", _string);
   };

The _string here is the equivalent of self->_string; so block is going to retain an internal object once. In other words, self will be retain one time. When Self is released, a block release is required to release the self, but the block release needs to wait for Self's dealloc to release. This changes to form a circular reference that results in a memory leak. The
 
Modification scheme is to create a local variable of __block scope, assign self to it, and use this local variable inside the block to take the value. Because __block-labeled variables are not automatically retain.
 

    __block viewcontroller *controller = self;
    _block = ^ () {
        NSLog (@ "string%@", controller- >_string);
   };

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.