我有一个包含AFHTTPSessionManager * fileTransferSessionManager的单例类。在其中我有时想要取消所有下载,在重新开始之前,这是通过downloadTasks运行并取消它们。问题是,当调用downloadtasks属性时,如果有正在运行的下载,则主线程会冻结。
// My method canceling download tasks:
NSArray *downloadTasks = self.fileTransferSessionManager.downloadTasks;
for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
[downloadTask cancel];
}
// In AFURLSessionManager.m
- (NSArray *)downloadTasks {
return [self tasksForKeyPath:NSStringFromSelector(_cmd)];
}
调用冻结主线程的方法,因为从不调用带有dispatch_semaphore_signal(信号量)的getTasksWithCompletionHandler:
// In AFURLSessionManager.m
- (NSArray *)tasksForKeyPath:(NSString *)keyPath {
__block NSArray *tasks = nil;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
if ([keyPath isEqualToString:NSStringFromSelector(@selector(dataTasks))]) {
tasks = dataTasks;
} else if ([keyPath isEqualToString:NSStringFromSelector(@selector(uploadTasks))]) {
tasks = uploadTasks;
} else if ([keyPath isEqualToString:NSStringFromSelector(@selector(downloadTasks))]) {
tasks = downloadTasks;
} else if ([keyPath isEqualToString:NSStringFromSelector(@selector(tasks))]) {
tasks = [@[dataTasks, uploadTasks, downloadTasks] valueForKeyPath:@"@unionOfArrays.self"];
}
dispatch_semaphore_signal(semaphore);
}];
NSLog(@"current thread: %@", [NSThread currentThread]);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return tasks;
}
我已将问题跟踪到self.session getTasksWithCompletionHandler:从不进入其块,因此dispatch_semaphore_wait将永远等待解锁主线程。
这可能是什么原因,我怀疑它是苹果框架中的一个错误,所以这就是为什么我在提交它之前问这里。我是否做过任何可能导致此问题的明显错误?