IOS开发之——单例-1次性代码(1)

一 概述

使用dispatch_once函数能保证某段代码在程序运行过程中只被执行1次

1
2
3
4
static dispatch_oncce_t onceToken;
dispatch_once(&onceToken,^{
//只执行1次的代码(这里面默认是线程安全的)
});

二 示例

2.1 示例一(变量控制)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,assign) BOOL hasExecuted;
@end

@implementation ViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if(self.hasExecuted)return;
NSLog(@"下载图片---------");
self.hasExecuted=YES;
}
@end

2.2 示例2(下载工具类)

下载工具类(ILImageDownloader)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import "ILImageDownloader.h"

@interface ILImageDownloader()
@property(nonatomic,assign) BOOL hasExecuted;
@end

@implementation ILImageDownloader

-(void)download
{
if (self.hasExecuted) return;
NSLog(@"下载图片-----");
self.hasExecuted=YES;
}
@end

ViewController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import "ViewController.h"
#import "ILImageDownloader.h"

@interface ViewController ()
@property(nonatomic,strong) ILImageDownloader *downloader;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.downloader=[[ILImageDownloader alloc]init];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.downloader download];
}
@end

2.3 示例三(dispatch_once)

下载工具类(ILImageDownloader)

1
2
3
4
5
6
7
8
9
#import "ILImageDownloader.h"

@implementation ILImageDownloader

-(void)download
{
NSLog(@"下载图片-----");
}
@end

ViewController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#import "ILImageDownloader.h"


@interface ViewController ()

@property(nonatomic,strong) ILImageDownloader *downloader;

@end

@implementation ViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ILImageDownloader *downloader=[[ILImageDownloader alloc]init];
[downloader download];
});
}
@end