#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