We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extension DispatchQueue { private static var _onceTracker = [String]() public class func once(token: String, block: () -> Void) { objc_sync_enter(self) defer { objc_sync_exit(self) } if _onceTracker.contains(token) { return } _onceTracker.append(token) block() } } extension NSObject { @discardableResult class func swizzleSelector(originalSelector: Selector, withSelector: Selector) -> Bool { guard let originalMethod = class_getInstanceMethod(self, originalSelector), let swizzledMethod = class_getInstanceMethod(self, withSelector) else { return false } let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)) if didAddMethod { class_replaceMethod(self, withSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)) } else { method_exchangeImplementations(originalMethod, swizzledMethod) } return true } } extension UIViewController { static func swizzleThreadTracking() { let originalSelector = #selector(viewDidLoad) let swizzledSelector = #selector(swizzled_viewDidLoad) UIViewController.swizzleSelector(originalSelector: originalSelector, withSelector: swizzledSelector) } @objc func swizzled_viewDidLoad() { // Call the original method first self.swizzled_viewDidLoad() // Now you can add your thread tracking code here DispatchQueue.once(token: "\(self.description) viewDidLoad", block: { print("\(String(describing: self)) called on thread: \(Thread.current.threadName) \(Thread.current.queueName)") }) } } class ThreadSwizzler { static let swizzleThread: () = { let originalSelector = #selector(getter: Thread.main) let swizzledSelector = #selector(swizzled_main) _ = NSObject.swizzleSelector(originalSelector: originalSelector, withSelector: swizzledSelector) }() @objc class func swizzled_main() -> Bool { print("Swizzled Thread.main called on thread: \(Thread.current)") return swizzled_main() } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: