Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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 (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-07T12:35:03.000Z (over 7 years ago)
- Last Synced: 2024-10-25T06:02:14.160Z (3 months ago)
- Topics: guard, ios, main, queue, thread, ui
- Language: Swift
- Homepage:
- Size: 485 KB
- Stars: 58
- Watchers: 3
- 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[![Version](https://img.shields.io/cocoapods/v/MainThreadGuard.svg?style=flat)](http://cocoapods.org/pods/MainThreadGuard)
[![License](https://img.shields.io/cocoapods/l/MainThreadGuard.svg?style=flat)](http://cocoapods.org/pods/MainThreadGuard)
[![Platform](https://img.shields.io/cocoapods/p/MainThreadGuard.svg?style=flat)](http://cocoapods.org/pods/MainThreadGuard)
![Swift](https://img.shields.io/badge/%20in-swift%203.0-orange.svg)![](Screenshots/Banner.png)
## 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 {
// setupGuard.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.whitelet 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, [email protected]
## License
MainThreadGuard is available under the MIT license. See the LICENSE file for more info.