objective-c - 在 Objective-C 中的函数调用后等待特定时间
问题描述
我有一个函数调用,调用后我需要等待 5 秒钟。我不等待使用[NSThread sleepForTimeInterval:5]
或简单sleep(5)
,因为它在主线程上运行,我最终会阻塞我的整个应用程序。
所需功能:
- 函数调用
- 睡5秒
- 继续处理其他功能
我如何在 Objective-C 上做到这一点?
解决方案
dispatch_after 怎么样?
https://developer.apple.com/documentation/dispatch/1452876-dispatch_after
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// throw your call to other functions in here
});
推荐阅读
- javascript - 为什么三级嵌套的 SortableJS 拖放不起作用?
- excel - Vlookup 正在返回应该显示值的空白单元格
- http - 纯 sh + telnet 中的 HTTP POST 文件:没有 curl,没有 wget
- python - 如何测试编辑数据库的 Flask 视图?
- javascript - 从填充的 var 获取 ajax 输出并在加载页面时打印出来
- javascript - 是否可以使 getter 方法异步?
- sql - 插入到带有日期时间列的表中
- hadoop - 从 HDFS 加载到 S3 时如何解压缩文件?
- eclipse - 我正在尝试在 Eclipse 中设置一个新项目给出错误消息:设置项目
- python - aws lambda - 找不到 libmagic