Check whether or not the current thread is the main thread

前端 未结 13 1035
北恋
北恋 2020-12-02 11:42

Is there any way to check whether or not the current thread is the main thread in Objective-C?

I want to do something like this.

  - (void)someMethod         


        
13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 12:18

    If you want to know whether or not you're on the main thread, you can simply use the debugger. Set a breakpoint at the line you're interested in, and when your program reaches it, call this:

    (lldb) thread info

    This will display information about the thread you're on:

    (lldb) thread info thread #1: tid = 0xe8ad0, 0x00000001083515a0 MyApp`MyApp.ViewController.sliderMoved (sender=0x00007fd221486340, self=0x00007fd22161c1a0)(ObjectiveC.UISlider) -> () + 112 at ViewController.swift:20, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1

    If the value for queue is com.apple.main-thread, then you're on the main thread.

提交回复
热议问题