{"id":18725088,"url":"https://github.com/indulgein/ybtaskscheduler","last_synced_at":"2025-10-14T13:08:23.654Z","repository":{"id":45052418,"uuid":"164173730","full_name":"indulgeIn/YBTaskScheduler","owner":"indulgeIn","description":"iOS 任务调度器，为 CPU 和内存减负（用于性能优化）","archived":false,"fork":false,"pushed_at":"2022-01-12T10:13:28.000Z","size":335,"stargazers_count":313,"open_issues_count":4,"forks_count":48,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-10-14T13:07:59.132Z","etag":null,"topics":["cpu","memory","optimizing-performance"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/indulgeIn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-05T02:16:25.000Z","updated_at":"2025-10-02T15:30:33.000Z","dependencies_parsed_at":"2022-08-25T13:50:38.490Z","dependency_job_id":null,"html_url":"https://github.com/indulgeIn/YBTaskScheduler","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/indulgeIn/YBTaskScheduler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indulgeIn%2FYBTaskScheduler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indulgeIn%2FYBTaskScheduler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indulgeIn%2FYBTaskScheduler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indulgeIn%2FYBTaskScheduler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/indulgeIn","download_url":"https://codeload.github.com/indulgeIn/YBTaskScheduler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indulgeIn%2FYBTaskScheduler/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279018700,"owners_count":26086604,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpu","memory","optimizing-performance"],"created_at":"2024-11-07T14:09:02.808Z","updated_at":"2025-10-14T13:08:23.637Z","avatar_url":"https://github.com/indulgeIn.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# YBTaskScheduler \n[![Cocoapods](https://img.shields.io/cocoapods/v/YBTaskScheduler.svg)](https://cocoapods.org/pods/YBTaskScheduler)\u0026nbsp;\n[![Cocoapods](https://img.shields.io/cocoapods/p/YBTaskScheduler.svg)](https://github.com/indulgeIn/YBTaskScheduler)\u0026nbsp;\n[![License](https://img.shields.io/github/license/indulgeIn/YBTaskScheduler.svg)](https://github.com/indulgeIn/YBTaskScheduler)\u0026nbsp;\n\niOS 任务调度器，为 CPU 和内存减负\n\n技术原理博客：[iOS 任务调度器：为 CPU 和内存减负](https://www.jianshu.com/p/f2a610c77d26)\n\n## 特性\n\n- 命令模式：将任务用容器管理起来延迟执行，实现任务执行频率控制、任务总量控制。\n- 策略模式：利用 C++ 栈、队列、优先队列实现三种调度策略，性能优越。\n- 应用场景一：主线程任务量过大导致掉帧（利用组件为任务调度降频）。\n- 应用场景二：短时间内执行的任务量过大，而某些任务失去了执行的意义（利用组件的任务淘汰策略）。\n- 应用场景三：需要将任务按自定义的优先级调度（利用组件的优先队列策略）\n\n## 安装\n\n### CocoaPods\n\n1. 在 Podfile 中添加 `pod 'YBTaskScheduler'`。\n2. 执行 `pod install` 或 `pod update`。\n3. 导入 `\u003cYBTaskScheduler/YBTaskScheduler.h\u003e`。\n\n若搜索不到库，可使用 rm ~/Library/Caches/CocoaPods/search_index.json 移除本地索引然后再执行安装，或者更新一下 CocoaPods 版本。\n\n### 手动导入\n\n1. 下载 YBTaskScheduler 文件夹所有内容并且拖入你的工程中。\n2. 导入 `YBTaskScheduler.h`。\n\n\n## 用法\n\n可下载 DEMO 参考一个相册处理的案例。\n\n### 基本使用\n\n```objc\n//初始化并选择任务调度策略\n_scheduler = [YBTaskScheduler schedulerWithStrategy:YBTaskSchedulerStrategyLIFO];\n//添加任务\n[_scheduler addTask:^{\n     /* \n     具体任务代码\n     解压图片、裁剪图片、访问磁盘等 \n     */\n}];\n```\n注意该组件使用实例化方式使用，为了避免任务调度器提前释放，需要外部对其进行强持有（建议作为调用方的属性或实例变量）。\n\n### 任务调度策略\n\n任务调度策略有三种：\n```objc\ntypedef NS_ENUM(NSInteger, YBTaskSchedulerStrategy) {\n    YBTaskSchedulerStrategyLIFO,    //后进先出（后进任务优先级高）\n    YBTaskSchedulerStrategyFIFO,    //先进先出（先进任务优先级高）\n    YBTaskSchedulerStrategyPriority   //优先级调度（自定义任务的优先级）\n};\n```\n首先要明确的是，业务中是想要执行`-addTask:`方法先添加的任务先调用，还是后添加的任务先调用。\n\n比如在一个 UITableView 的列表中，每一个 Cell 都有一个将头像图片异步裁剪为圆角的任务，当快速滑动的时候，理所应当是后加入的任务应该调用，所以应该选择 YBTaskSchedulerStrategyLIFO。\n\n当业务中的任务需要按照你自己指定的优先级来调度，就选择 YBTaskSchedulerStrategyPriority。\n\n### 任务执行线程队列\n\n若不显式的指定任务执行队列，组件会默认让这些任务并发执行（类似并行队列）。\n你可以指定执行的队列，比如你只是想降低主线程任务的调用频率：\n```objc\n_scheduler.taskQueue = dispatch_get_main_queue();\n```\n如此，所有添加的任务都会在主队列执行。\n\n### 任务淘汰机制\n\n很多时候你需要淘汰掉一部分已经添加到`YBTaskScheduler`的任务。\n\n比如上面举得在 UITableView 的 cell 中异步裁剪头像图片的例子，当用户快速滑动时，假设有 100 个任务添加到了任务调度器中，而前 90 个 cell 已经滑出了屏幕，它们的异步任务已经不需要了（建立在你不会缓存异步结果的前提下）：\n```objc\n_scheduler.maxNumberOfTasks = 20;\n```\n这里设置最大任务数量为 20，若添加的任务大于了这个数量，会删除掉优先级低的任务。\n而不同的任务调度策略有不同的效果，比如 YBTaskSchedulerStrategyLIFO 策略会删除先加入的任务，YBTaskSchedulerStrategyFIFO 策略会删除后加入的任务，注意 YBTaskSchedulerStrategyPriority 暂时不支持任务淘汰机制（由于 C++ 优先队列不支持低优先级节点删除，后面考虑自己实现）。\n\n### 任务调用频率控制\n\n以下两个属性就能控制频率：\n```objc\n/* 每次执行的任务数量 */\n@property (nonatomic, assign) NSUInteger executeNumber;\n/* 执行频率（RunLoop 循环 executeFrequency 次执行一次任务） */\n@property (nonatomic, assign) NSUInteger executeFrequency;\n```\n默认是每次 RunLoop 循环调用一个任务，比如你这么做：\n```objc\n_scheduler.executeNumber = 2;\n_scheduler.executeFrequency = 5;\n```\n那么就表示 RunLoop 循环 5 次调用 2 个任务。\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Findulgein%2Fybtaskscheduler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Findulgein%2Fybtaskscheduler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Findulgein%2Fybtaskscheduler/lists"}