{"id":20237778,"url":"https://github.com/fantasticlbp/mainthreadchecker","last_synced_at":"2025-04-10T19:12:14.206Z","repository":{"id":97340285,"uuid":"455568391","full_name":"FantasticLBP/MainThreadChecker","owner":"FantasticLBP","description":"子线程 UI 监控","archived":false,"fork":false,"pushed_at":"2022-06-29T08:42:17.000Z","size":34,"stargazers_count":9,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T16:53:40.264Z","etag":null,"topics":["apm","app","ios","runloop","ui","uikit"],"latest_commit_sha":null,"homepage":"https://github.com/FantasticLBP/knowledge-kit/blob/master/Chapter1%20-%20iOS/1.74.md","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/FantasticLBP.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2022-02-04T14:01:48.000Z","updated_at":"2023-03-16T07:49:04.000Z","dependencies_parsed_at":"2023-06-30T00:45:42.214Z","dependency_job_id":null,"html_url":"https://github.com/FantasticLBP/MainThreadChecker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FantasticLBP%2FMainThreadChecker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FantasticLBP%2FMainThreadChecker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FantasticLBP%2FMainThreadChecker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FantasticLBP%2FMainThreadChecker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FantasticLBP","download_url":"https://codeload.github.com/FantasticLBP/MainThreadChecker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248279865,"owners_count":21077408,"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","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":["apm","app","ios","runloop","ui","uikit"],"created_at":"2024-11-14T08:28:49.623Z","updated_at":"2025-04-10T19:12:14.202Z","avatar_url":"https://github.com/FantasticLBP.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MainThreadChecker\n\n一款监控子线程操纵 UI 的能力，也可以添加自定义的 API 进行监控（实现在子线程监控某些 API 的时候捕获具体堆栈信息，帮助定位问题）\n\n### 背景介绍\n\n可能有些人一直没有遇到过因为在子线程操作 UI，导致在开发阶段 Xcode console 输出了一堆日志，大体如下\n\n![](https://raw.githubusercontent.com/FantasticLBP/knowledge-kit/master/assets/2022-0204-SubThreadUIXcode1@2x.png)\n\n其实我们可以给 Xcode 打个 `Runtime Issue Breakpoint` ，type 选择 `Main Thread Checker`， 在发生子线程操作 UI 的时候就会被系统检测到并触发断点，同时可以看到堆栈情况\n\n![](https://raw.githubusercontent.com/FantasticLBP/knowledge-kit/master/assets/2022-0204-SubThreadUISymbolBreakpoints@2x.png)\n\n效果如下\n\n![](https://raw.githubusercontent.com/FantasticLBP/knowledge-kit/master/assets/2022-0204-SubThreadUIXcodeBreakingPointsHappened@2x.png)\n\n### 问题及解决方案\n\n上述的功能是在 Xcode 自带的，连接 Xcode 做调试才具备的功能，线上包无法检测到。\n\n经过探索 Xcode 实现该功能是依赖于设备上的` libMainThreadChecker.dylib` 库，我们可以通过 `dlopen` 方法强制加载该库让非 Xcode 环境下也拥有监测功能。\n\n另外在监控到子线程调用 UI 调用时，在 Xcode 环境下，会将调用栈输出到控制台，经过测试，`libMainThreadChecker.dylib` 使用的是进行输出的，由于 NSLog 是将信息输出到 `STDERR`中，我们可以通过 `NSPipe` 与 `dup2` 将 `STDERR` 输出拦截，通过对信息的文案的判断，进而获取监测到的 UI 调用，最后可以通过堆栈打印出来，就可以帮助定位到具体问题。\n\n`libMainThreadChecker.dylib` 库具有局限性，仅仅对系统提供的一些特定类的特定 API 在子线程调用会被监控到（例如 UIKit 框架中 UIView 类）。\n但是某些类有些 API 我们也不希望在子线程被调用，这时候 `libMainThreadChecker.dylib`是无法满足的。\n\n对 `libMainThreadChecker.dylib` 库的汇编代码研究，发现 `libMainThreadChecker.dylib` 是通过内部 `__main_thread_add_check_for_selector` 这个方法来进行类和方法的注册的。所以如果我们同样可以通过 `dlsym` 来调用该方法，以达到对自定义类和方法的主线程调用监测。\n![](https://raw.githubusercontent.com/FantasticLBP/knowledge-kit/master/assets/2022-0204-SubThreadUIMonitor@2x.PNG)\n\n另外该功能可以在线下 debug 阶段开启，判断是否是在 Xcode debug 状态，可以通过苹果提供的[官方判断方法](https://developer.apple.com/library/archive/qa/qa1361/_index.html#//apple_ref/doc/uid/DTS10003368)实现。\n\n对 [dlopen](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlopen.3.html)、[dlsym](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html) 陌生的小伙伴可以直接看 Apple 官方文档，这里不做展开。","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffantasticlbp%2Fmainthreadchecker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffantasticlbp%2Fmainthreadchecker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffantasticlbp%2Fmainthreadchecker/lists"}