ios - NSData 结果导致 NSTimer 延迟
问题描述
我有一个 NSTimer 应该在 10 秒后触发它的选择器。但是由于 NSData 结果的延迟,计时器也被延迟,并且在执行 NSData 操作后触发其选择器。
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(fireTimer)
userInfo:nil
repeats:NO];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:decryptedPath]];
_audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
在上面的代码中,Timer 等待 NSData 结果然后触发它的选择器。
解决方案
这是因为dataWithContentsOfURL:
阻塞了主队列(这是计时器触发的地方)。你不应该那样做。您需要先使用 NSURLSession 下载它,一旦完成,然后将数据传递给 AVAudioPlayer。AVAudioPlayer 不是为流媒体设计的。
推荐阅读
- python - 在 API 调用中填写表单的 Python Bot
- sharepoint-online - SharePoint Online - 页面模板
- antlr - Antlr 生产产生一堆单元素数组
- bootstrap-5 - Bootstrap 5 导航栏下拉菜单离开屏幕
- mysql - 如何解决 MySQL 显示不存在的数据?
- python - 根据每个列类型获取数据框的平均值和模式
- python - 使用 Python 编织 RMarkdown 文档时出错
- javascript - 带有空格属性的元素的Javascript选择器
- automated-tests - 空手道 -使用 Javascript从元素获取 URL
- amazon-athena - AWS Athena 结构不解析 JSON 字符串