use*_*354 7 iphone nstimer grand-central-dispatch ios
我正在开发一个应用程序,我想在单独的队列中使用调用方法dispatch_async.我希望在一定的时间间隔后重复调用该方法.但是方法没有被调用.
我不知道什么是错的.这是我的代码:
dispatch_async( NotificationQueue, ^{
NSLog(@"inside queue");
timer = [NSTimer scheduledTimerWithTimeInterval: 20.0
target: self
selector: @selector(gettingNotification)
userInfo: nil
repeats: YES];
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
});
});
-(void)gettingNotification {
NSLog(@"calling method ");
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*Rob 10
如果你想成为一个调用重复的计时器dispatch_queue_t,使用dispatch_source_create了DISPATCH_SOURCE_TYPE_TIMER:
dispatch_queue_t queue = dispatch_queue_create("com.firm.app.timer", 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 20ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
// stuff performed on background queue goes here
NSLog(@"done on custom background queue");
// if you need to also do any UI updates, synchronize model updates,
// or the like, dispatch that back to the main queue:
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"done on main queue");
});
});
dispatch_resume(timer);
Run Code Online (Sandbox Code Playgroud)
这创建了一个每20秒运行一次的计时器(第3个参数dispatch_source_set_timer),余地为1秒(第4个参数dispatch_source_set_timer).
要取消此计时器,请使用dispatch_source_cancel:
dispatch_source_cancel(timer);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7314 次 |
| 最近记录: |