[go: up one dir, main page]

Skip to content
New issue

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

Feature: - Create a Main thread checker (Send alert if view or viewController is not called in main thread) #67

Open
MaatheusGois opened this issue Apr 30, 2024 · 0 comments
Labels
feature 💡 Feature to develop.

Comments

@MaatheusGois
Copy link
Member
MaatheusGois commented Apr 30, 2024
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()
    }
}
@MaatheusGois MaatheusGois added the feature 💡 Feature to develop. label Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 💡 Feature to develop.
Projects
None yet
Development

No branches or pull requests

1 participant