https://github.com/onmyway133/mainthreadguard
:guardsman: Tracking UIKit access on main thread
https://github.com/onmyway133/mainthreadguard
guard ios main queue thread ui
Last synced: 3 months ago
JSON representation
:guardsman: Tracking UIKit access on main thread
- Host: GitHub
- URL: https://github.com/onmyway133/mainthreadguard
- Owner: onmyway133
- License: other
- Created: 2016-01-16T04:58:27.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-06-07T12:35:03.000Z (almost 9 years ago)
- Last Synced: 2025-09-12T06:34:27.655Z (9 months ago)
- Topics: guard, ios, main, queue, thread, ui
- Language: Swift
- Homepage: https://onmyway133.com
- Size: 485 KB
- Stars: 59
- Watchers: 2
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# MainThreadGuard
Tracking UIKit access on main thread
[](http://cocoapods.org/pods/MainThreadGuard)
[](http://cocoapods.org/pods/MainThreadGuard)
[](http://cocoapods.org/pods/MainThreadGuard)


## Description
This is just a Swift port of [PSPDFUIKitMainThreadGuard.m](https://gist.github.com/steipete/5664345) using swizzling on `UIView` extension
## Usage
Call `Guard.setup`
```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// setup
Guard.setup()
return true
}
```
## Features
It works by swizzling in the following methods
- setNeedsLayout
- setNeedsDisplay
- setNeedsDisplayInRect:
Try accessing UIKit from another thread and MainThreadGuard will assert
```swift
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
let label = UILabel()
view.addSubview(label)
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
label.text = "Setting text on background thread"
}
}
}
```
## Xcode 9+
Xcode 9+ has [Main Thread Checker](https://developer.apple.com/documentation/code_diagnostics/main_thread_checker), which detects invalid use of AppKit, UIKit, and other APIs from a background thread.
## Installation
MainThreadGuard is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
You should add this to Debug configuration only
```ruby
pod "MainThreadGuard", git: 'https://github.com/onmyway133/MainThreadGuard', configurations: 'Debug'
```
## Author
Khoa Pham, onmyway133@gmail.com
## License
MainThreadGuard is available under the MIT license. See the LICENSE file for more info.