Function objc2_foundation::run_on_main

source ·
pub fn run_on_main<F, R>(f: F) -> R
where F: Send + FnOnce(MainThreadMarker) -> R, R: Send,
Available on crate features NSThread and dispatch only.
Expand description

Submit the given closure to the runloop on the main thread.

If the current thread is the main thread, this runs the closure.

The closure is passed a MainThreadMarker that it can further use to access APIs that are only accessible from the main thread.

This function should only be used in applications whose main thread is running an event loop with dispatch_main, UIApplicationMain, NSApplicationMain, CFRunLoop or similar; it will block indefinitely if that is not the case.

§Example

use objc2_foundation::run_on_main;
run_on_main(|mtm| {
    // Do something on the main thread with the given marker
});