2013-01-09 173 views
0

說我連接一個IBAction與xib文件中的按鈕。當應用程序運行時,我點擊按鈕,這個動作是同步調用還是在runloop上放入一個performSelector請求?Cocoa:IBAction回調是同步的還是異步的?

+1

在你的動作中設置一個斷點。點擊按鈕,然後查看正在調用您的操作的回溯中的方法。它看起來像是被該代碼同步調用的,還是看起來像是由延遲的'performSelector'調用的? –

+0

@KurtRevis我認爲它是異步的,它看起來像「異步」,我只是想確保我在這裏問。 – CarmeloS

回答

2

它是同步的。

在iOS應用程序,回溯看起來像:

frame #0: 0x000025d2 MyApp`-[ViewController myAction:] + 34 at ViewController.m:31 
frame #1: 0x010e2705 libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 77 
frame #2: 0x00019920 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96 
frame #3: 0x000198b8 UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61 
frame #4: 0x000da671 UIKit`-[UIControl sendAction:to:forEvent:] + 66 
frame #5: 0x000dabcf UIKit`-[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578 
frame #6: 0x000d9d38 UIKit`-[UIControl touchesEnded:withEvent:] + 546 
frame #7: 0x0004933f UIKit`-[UIWindow _sendTouchesForEvent:] + 846 
frame #8: 0x00049552 UIKit`-[UIWindow sendEvent:] + 273 
frame #9: 0x000273aa UIKit`-[UIApplication sendEvent:] + 436 
frame #10: 0x00018cf8 UIKit`_UIApplicationHandleEvent + 9874 
frame #11: 0x01becdf9 GraphicsServices`_PurpleEventCallback + 339 
frame #12: 0x01becad0 GraphicsServices`PurpleEventCallback + 46 
frame #13: 0x01c06bf5 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53 
frame #14: 0x01c06962 CoreFoundation`__CFRunLoopDoSource1 + 146 
frame #15: 0x01c37bb6 CoreFoundation`__CFRunLoopRun + 2118 
frame #16: 0x01c36f44 CoreFoundation`CFRunLoopRunSpecific + 276 
frame #17: 0x01c36e1b CoreFoundation`CFRunLoopRunInMode + 123 
frame #18: 0x01beb7e3 GraphicsServices`GSEventRunModal + 88 
frame #19: 0x01beb668 GraphicsServices`GSEventRun + 104 
frame #20: 0x0001665c UIKit`UIApplicationMain + 1211 
frame #21: 0x00001f62 MyApp`main + 130 at main.m:16 
frame #22: 0x00001e95 MyApp`start + 53 

這是直行同步碼。應用程序接收一個觸摸結束事件,確定將它分派到哪裏(按鈕),並向該按鈕的目標發送消息。

相反,異步調用方法,通過-[NSObject performSelector:withObject:afterDelay:],有這樣的回溯:

frame #0: 0x00002628 MyApp`-[ViewController myAsyncAction:] + 24 at ViewController.m:37 
frame #1: 0x00acce83 Foundation`__NSFireDelayedPerform + 380 
frame #2: 0x01c50376 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22 
frame #3: 0x01c4fe06 CoreFoundation`__CFRunLoopDoTimer + 534 
frame #4: 0x01c37a82 CoreFoundation`__CFRunLoopRun + 1810 
frame #5: 0x01c36f44 CoreFoundation`CFRunLoopRunSpecific + 276 
frame #6: 0x01c36e1b CoreFoundation`CFRunLoopRunInMode + 123 
frame #7: 0x01beb7e3 GraphicsServices`GSEventRunModal + 88 
frame #8: 0x01beb668 GraphicsServices`GSEventRun + 104 
frame #9: 0x0001665c UIKit`UIApplicationMain + 1211 
frame #10: 0x00001f62 MyApp`main + 130 at main.m:16 
frame #11: 0x00001e95 MyApp`start + 53 

你可以清楚地看到,一個計時器火災和調用它的回調,而「延遲執行」在上面建那是被觸發的。

+0

謝謝,但我認爲performSelector是異步的,無論它是否有延遲參數。 performSelector是通過runloop拾取的,而不是CPU上的直接跳轉指令,這就是我所說的異步,也許我們有一些誤解。 – CarmeloS

+0

你在說什麼'performSelector'方法? ' - [NSObject performSelector:]',並且具有一個或兩個'object'參數的變體同步發生,而不經過運行循環。 [「performSelector:方法等同於直接向接收方發送aSelector消息。」](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject .html#// apple_ref/occ/intfm/NSObject/performSelector :)' - [NSObject performSelector:withObject:afterDelay:]'異步發生(當被運行循環觸發時)。 –

+0

非常感謝你,我終於明白了。 performSelector在NSObject協議中定義,performSelector:afterDelay在NSObject類中。 – CarmeloS

相關問題