mai

MainThreadGuard

πŸ’‚β€β™‚οΈ Tracking UIKit access on main thread

Showing:

Popularity

Downloads/wk

0

GitHub Stars

56

Maintenance

Last Commit

4yrs ago

Contributors

0

Package

Dependencies

0

License

MIT

Categories

Readme

MainThreadGuard

Tracking UIKit access on main thread

Version License Platform Swift

Description

This is just a Swift port of PSPDFUIKitMainThreadGuard.m using swizzling on UIView extension

Usage

Call Guard.setup

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

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, which detects invalid use of AppKit, UIKit, and other APIs from a background thread.

Installation

MainThreadGuard is available through CocoaPods. To install it, simply add the following line to your Podfile:

You should add this to Debug configuration only

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.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100