{"id":20714014,"url":"https://github.com/dustturtle/launch-task","last_synced_at":"2026-03-17T04:39:52.047Z","repository":{"id":74191533,"uuid":"604460241","full_name":"dustturtle/launch-task","owner":"dustturtle","description":null,"archived":false,"fork":false,"pushed_at":"2023-02-20T15:38:17.000Z","size":67,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-12-26T16:38:54.696Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"jayden320/launch-task","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dustturtle.png","metadata":{"files":{"readme":"README-ZH.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-21T05:22:39.000Z","updated_at":"2023-02-21T05:19:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"b6b286e6-1e4c-4a36-977f-e9152e8c5757","html_url":"https://github.com/dustturtle/launch-task","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dustturtle/launch-task","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustturtle%2Flaunch-task","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustturtle%2Flaunch-task/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustturtle%2Flaunch-task/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustturtle%2Flaunch-task/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dustturtle","download_url":"https://codeload.github.com/dustturtle/launch-task/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dustturtle%2Flaunch-task/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30610393,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-16T23:44:20.790Z","status":"online","status_checked_at":"2026-03-17T02:00:07.764Z","response_time":56,"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":[],"created_at":"2024-11-17T02:28:42.727Z","updated_at":"2026-03-17T04:39:52.032Z","avatar_url":"https://github.com/dustturtle.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# LaunchTask\n\nLanguage: [English](README.md) | 中文\n\nLaunchTask 是一个管理 App 启动任务的框架。\n\n一个大型的 App 往往需要在启动阶段执行大量的任务。如果 App 的开发人员很多，容易导致 AppDelegate 的代码频繁修改，越来越臃肿。也容易导致 App 的启动时长变长，最终被 watchdog 杀死。\n\nLaunchTask 是将启动的代码封装到一个个 task 的子类中，通过 workflow 配置 task 的执行顺序和依赖。同时通过并发执行任务，加快启动时间。\n\n## Installation\n通过 CocoaPods 安装 LaunchTask。\n```\npod 'LaunchTask'\n```\n\n## Using LaunchTask\n\nLaunchTask 库中主要有两个概念：`Task` 和 `Workflow`。二者的关系类似于 `NSOperation` 和 `NSOperationQueue`。Task 主要用于执行具体的任务。Workflow 则用于管理多个 Task 的执行顺序。\n\n首先，我们需要创建一个 workflow 的实例对象：\n```Swift\nlet workflow = TaskWorkflow(name: \"Launch\")\n```\n创建完 workflow 后，我们需要创建需要执行的 Task。Task 有三种创建方式：\n* 子类化 LaunchTask。\n```Swift\nclass SomeTask: LaunchTask {\n    override func main(context: [AnyHashable: Any]?) {\n        // Perform specific tasks\n    }\n}\nlet task  = SomeTask()\n```\n* 创建 ClosureTask 实例，传入需要执行的 Closure。\n```Swift\nlet task = ClosureTask(name: \"TaskName\") { context in\n    // Perform specific tasks\n}\n```\n* 创建 SelectorTask 实例，传入 target 和 selector。\n```Swift\nlet task = SelectorTask(name: \"TaskName\", target: self, selector: #selector(someFunction))\n```\n创建完 Task 实例后，需要将 Task 添加到 workflow 中。\n```Swift\nworkflow.addTask(task)\n```\n最后，在需要执行 workflow 的地方，调用 start 方法。\n```Swift\nworkflow.start()\n```\n假设任务 C 需要等待前面的并发任务（A、B）全部完成，则可以使用 workflow 的 addBlockingTasks 方法。\n```\n              ╔═════╗\n          ┌──\u003e║  A  ║───┐\n          │   ╚═════╝   │\n┌─────┐   │             │   ╔═════╗    ┌──────┐\n│Start│───┤             ├──\u003e║  C  ║───\u003e│Finish│\n└─────┘   │             │   ╚═════╝    └──────┘\n          │   ╔═════╗   │\n          └──\u003e║  B  ║───┘\n              ╚═════╝\n```\n具体的代码如下：\n```\nworkflow.addBlockingTasks([\n    TaskA(),\n    TaskB(queue: .concurrentQueue),\n])\nworkflow.addTask(TaskC())\nworkflow.start()\n```\n\n## Example\n\n假设在 App 启动的时候，需要执行下图中的初始化步骤。\n```\n                                 ╔═════╗   ╔═════╗\n                              ┌─\u003e║  C  ║──\u003e║  D  ║─┐\n                              │  ╚═════╝   ╚═════╝ │\n┌─────┐   ╔═════╗   ╔═════╗   │                    │   ╔═════╗   ╔═════╗   ┌──────┐\n│Start│──\u003e║  A  ║──\u003e║  B  ║──\u003e│                    ├──\u003e║  G  ║──\u003e║  H  ║──\u003e│Finish│\n└─────┘   ╚═════╝   ╚═════╝   │                    │   ╚═════╝   ╚═════╝   └──────┘\n                              │       ╔═════╗      │\n                              ├──────\u003e║  E  ║──────┤\n                              │       ╚═════╝      │\n                              │                    │\n                              │       ╔═════╗      │\n                              └──────\u003e║  F  ║──────┘\n                                      ╚═════╝\n```\n对应的代码实现如下：\n```Swift\nlet workflow = TaskWorkflow(name: \"Launch\")\nworkflow.delegate = self\nworkflow.addTask(TaskA())\nworkflow.addTask(TaskB())\nworkflow.addBlockingTasks([\n    TaskC(),\n    TaskD(),\n    TaskE(queue: .concurrentQueue),\n    TaskF(queue: .concurrentQueue),\n])\nworkflow.addTask(TaskG())\nworkflow.addTask(TaskH())\nworkflow.start()\n```\n在上面的代码中，TaskG 会等待 BlockingTasks 中的任务全部执行完成后，才会继续执行。我们可以在 TaskG 中创建 App 的 RootViewController。在 BlockingTasks 中并发执行耗时的操作，比如预加载首页需要用到的资源，初始化重要的 SDK 等。\n如果想查 workflow 的执行时间线，可以在 workflow 结束时，生成时间线:\n```Swift\nfunc workflowDidFinish(_ workflow: TaskWorkflow) {\n    print(workflow.generateTimeline())\n}\n```\n时间线如下\n```\n[Launch start] - [TaskA 201ms]  - [TaskB 201ms]  - [TaskC 200ms]  - [TaskD 201ms]  - [Waiting 0ms]  - [TaskG 13ms]  - [TaskH 201ms]  - [Launch finish]\n                                - [TaskE 105ms]\n                                - [TaskF 205ms]\n```\n\n## Author\n\nJayden Liu\n\n## License\n\nLaunchTask is available under the MIT license. See the LICENSE file for more info.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdustturtle%2Flaunch-task","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdustturtle%2Flaunch-task","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdustturtle%2Flaunch-task/lists"}