Self-strong reference problem in multi-threaded block

Source: Internet
Author: User
Tags gcd

#import "ViewController.h"

@interface Viewcontroller ()

Defines a global queue property. This queue is easy to use in any method

@property (Nonatomic,strong) Nsoperationqueue *queue;

UI controls have no problem with weak and strong.

In development, the basic meeting with all UI controls is done with strong.

UI controls are generally not loaded with lazy loading. UI controls are not the same as the user's. UI controls, and can be lazy loaded with lazy loading.

@property (Nonatomic,strong) UIButton *button;

@end

@implementation Viewcontroller

Lazy Loading

-(Nsoperationqueue *) queue

{

if (!_queue) {

_queue = [[Nsoperationqueue alloc] init];

Sets the maximum number of concurrent threads. At the same time, you can only open 6 thread.

[_queue Setmaxconcurrentoperationcount:6];

Network factors:

}

return _queue;

}

-(void) Viewdidload {

[Super Viewdidload];

}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

{

Using self directly in the block will cause circular references. One of the points of note used by block: circular reference.

__weak typeof (self) wself = self; Wself is the weak citation of self.

The tasks in GCD are encapsulated in a block, and if self is present in the block in GCD, will it cause a circular reference?

//

Dispatch_async (Dispatch_get_main_queue (), ^{

[Self test];

//    }); Will it cause a circular reference? does not cause a circular reference, because self does not have a strong reference to the block.

Create action

__weak typeof (self) wself = self;

Nsblockoperation *op = [Nsblockoperation blockoperationwithblock:^{

Self is present in block. Block is a strong reference to self.

[Wself test];

}];

Adds an action to the queue.

Self.queue is a strong reference to Op.

[Self.queue Addoperation:op];

}

When a memory warning is received, it is called.

-(void) didreceivememorywarning

{

[Super didreceivememorywarning];

Cancels the operation. There are two methods:

1> operation Queue cancels all operations

Cancels all operations. For canceled operations, recovery cannot be resumed.

[Self.queue cancelalloperations];

2> a single operation can be canceled. Method of Nsoperation.

}

-(void) test1

{

Nsoperation Advanced Operations: Pause/Resume

These two methods are generally used when interacting with the user.

Pause all operations in a queue

[Self.queue Setsuspended:yes];

Recover all operations in a queue

[Self.queue Setsuspended:no];

}

-(void) test

{

NSLog (@ "Download task (very time consuming)------------%@", [Nsthread CurrentThread]);

}

@end

Self-strong reference problem in multi-threaded 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.