【发布时间】:2014-06-06 10:34:07
【问题描述】:
我创建了一个队列,我在其中执行打印后台任务:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_queue = dispatch_queue_create("q.myqueue.com", nil);
dispatch_set_finalizer_f(self.queue, &myFinalizerFunction);
background = UIBackgroundTaskInvalid;
t = nil;
[self beginBackgroundTask];
}
- (void)beginBackgroundTask {
// Start the long-running task and return immediately.
dispatch_async(self.queue, ^{
UIApplication *app = [UIApplication sharedApplication];
background = [app beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundTask];
}];
NSLog(@"background created: %d", background);
t = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(printIt)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:t
forMode:NSRunLoopCommonModes]; // NSDefaultRunLoopMode
[[NSRunLoop currentRunLoop] run];
});
}
- (void)endBackgroundTask {
NSLog(@"Ending background");
if (t!=nil) {
[t invalidate];
t = nil;
}
[[UIApplication sharedApplication] endBackgroundTask:background];
background = UIBackgroundTaskInvalid;
NSLog(@"Ended!");
}
后台过期后,调用[self endBackgroundTask];但是当我通过“暂停程序执行”监视 Xcode 调试时,我仍然看到队列“q.myqueue.com”!
Thread 2
Queue: q.myqueue.com
0 mach_msg_trap
7 -[NSRunLoop(NSRunLoop) run]
8 __37-[ViewController beginBackgroundTask]_block_invoke
9 _dispatch_call_block_and_release
14 _pthread_wqthread
谁能解释为什么后台任务过期后Xcode调试中队列“q.myqueue.com”仍然存在?我想使用dispatch_release(self.queue),但 ARC 拒绝了。
谢谢
【问题讨论】:
标签: xcode queue automatic-ref-counting release