2013-05-10 17:31:09 u010013695 阅读数 3856

1 前言

使用 dispatch_once 函数 在 APP 的生命周期内来保证你想确保每段代码只执行一次,即使它在代码的不同地方多次调用(比如单例的初始化)。

2 代码实例

ZYAppDelegate.m

//一个用于调度一次函数的标识
static dispatch_once_t onceToken;
//Block Object
void (^executedOnlyOnce)(void) = ^{
    static NSUInteger numberOfEntries = 0;
    numberOfEntries++;
    NSLog(@"Executed %lu time(s)", (unsigned long)numberOfEntries);
};
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //声明一个队列
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //执行一次的队列
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

ZYViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    ZYMySingleton *test = [[ZYMySingleton alloc] init];
    //循环单例方法
    for (int i=0; i<5; i++) {
        [test sharedInstance];
    }
    [test release];
}

ZYMySingleton.m

- (id) sharedInstance{
    static ZYMySingleton *SharedInstance = nil;
    //一个用于调度一次函数的标识
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SharedInstance = [ZYMySingleton new];
        NSLog(@"SharedInstance is ======>%@",SharedInstance);
    });
    return SharedInstance;
}

运行后控制台显示结果

2013-05-10 17:30:49.334 GCDSingleTest[2717:1303] Executed 1 time(s)

2013-05-10 17:30:49.336 GCDSingleTest[2717:c07] SharedInstance is ======><ZYMySingleton: 0x7195990>

3 结语

以上是所有内容,希望对大家有所帮助

Demo代码下载:http://download.csdn.net/detail/u010013695/5353668

2018-03-14 21:25:58 Margaret_MO 阅读数 984

// 只执行最后一次

[NSObject cancelPreviousPerformRequestsWithTarget:self];

[self performSelector:@selector(realClick) withObject:nil afterDelay:5];

 

// 只执行一次

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

    NSLog(@"once times");

});

2015-11-11 17:39:00 weixin_30544657 阅读数 133

IOS开发(64)之GCD任务最多只执行一次

1 前言

使用 dispatch_once 函数 在 APP 的生命周期内来保证你想确保每段代码只执行一次,即使它在代码的不同地方多次调用(比如单例的初始化)。

 

2 代码实例
ZYAppDelegate.m

 

[plain]
/一个用于调度一次函数的标识
static dispatch_once_t onceToken;
//Block Object
void (^executedOnlyOnce)(void) = ^{
    static NSUInteger numberOfEntries = 0;
    numberOfEntries++;
    NSLog(@"Executed %lu time(s)", (unsigned long)numberOfEntries);
};
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //声明一个队列
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //执行一次的队列
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

//一个用于调度一次函数的标识
static dispatch_once_t onceToken;
//Block Object
void (^executedOnlyOnce)(void) = ^{
    static NSUInteger numberOfEntries = 0;
    numberOfEntries++;
    NSLog(@"Executed %lu time(s)", (unsigned long)numberOfEntries);
};
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //声明一个队列
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //执行一次的队列
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
ZYViewController.m

 

[plain]
- (void)viewDidLoad
{
    [super viewDidLoad];
    ZYMySingleton *test = [[ZYMySingleton alloc] init];
    //循环单例方法
    for (int i=0; i<5; i++) {
        [test sharedInstance];
    }
    [test release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    ZYMySingleton *test = [[ZYMySingleton alloc] init];
    //循环单例方法
    for (int i=0; i<5; i++) {
        [test sharedInstance];
    }
    [test release];
}
ZYMySingleton.m

 

[plain] view plaincopyprint?- (id) sharedInstance{
    static ZYMySingleton *SharedInstance = nil;
    //一个用于调度一次函数的标识
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SharedInstance = [ZYMySingleton new];
        NSLog(@"SharedInstance is ======>%@",SharedInstance);
    });
    return SharedInstance;
}

- (id) sharedInstance{
    static ZYMySingleton *SharedInstance = nil;
    //一个用于调度一次函数的标识
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SharedInstance = [ZYMySingleton new];
        NSLog(@"SharedInstance is ======>%@",SharedInstance);
    });
    return SharedInstance;
}
运行后控制台显示结果

转载于:https://www.cnblogs.com/lidongxiao/p/4956631.html

2016-03-27 00:25:35 feng2qing 阅读数 3194

在实际开发中,有时候需要某一块代码自程序启动开始只执行一次,当然实现实现的方法有很多,比如可以设置BOOL的标记,但是还有一个简单高效的办法,用dispatch_once可以实现,如下:


static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{


    NSLog(@"只打印一次");

});

2017-07-21 10:53:58 cizhaoyang 阅读数 1518
使用这个函数来实现
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;

例子:
dic是要传的参数,我的是一个字典类型
[self performSelectorOnMainThread:@selector(sendData:) withObject:dic waitUntilDone:NO];

//实现方法
-(void)sendData: (NSDictionary *)dic {
[self performSelector:@selector(send:) withObject:dic afterDelay:10];
}

-(void)send: (NSDictionary *)dic {
NSLog(@”%@”,dic);
}

没有更多推荐了,返回首页