2010-03-24 179 views
3

是什麼以下代碼NSthread和NStimer和NSNotifcation之間的區別?

1)

[NSThread detachNewThreadSelector:@selector(myFunction) toTarget:self withObject:thename]; 

2)

[NSTimer scheduledTimerWithTimeInterval:1.0 
           target:self 
           selector:@selector(myFunction:) 
           userInfo:nil 
           repeats:NO]; 

3)

[self performSelector:@selector(myFunction) withObject:nil afterDelay:myDelay]; 

4)

之間的差210

回答

6
  1. 在單獨的線程中執行選擇器。如果要在被調用函數中自動釋放對象,則需要設置自己的自動釋放池。您不能直接使用GUI,但如果功能花費太長時間才能完成,則不會阻止它。使用performSelectorInBackground:NSObject可能寫得更好。

  2. 在主線程上延遲後運行選擇器。無需使用自動釋放池(您正在使用默認的池),您可以直接使用GUI,但是如果功能需要很長時間才能完成,則會阻止它。

  3. 非常喜歡2.

  4. 完全不同的東西,看到的文檔NSNotificationCenter。您告訴默認通知中心,您希望收到由nil(=任何對象)發送的名稱爲thename的所有通知。當發佈此類通知時,通知中心將致電myFunction並向其傳遞描述事件的NSNotification實例。

我希望我的一切都正確,前三點有點棘手。

+0

你說performSelectorInBackground更好,因爲從NSObject.but detachNewThreadSelector(NSThread.h),它也是從NSObject.the doc派生的(http://stackoverflow.com/questions/6539460/what-is-the-difference- between-nsthread-detachnewthreadselectortotargetwithob/6883442#6883442)表示兩者都是相同的,然後區別只是autorelease池。 performSelectorInBackground是自動創建pool.but detach .. not.am我是嗎?有沒有任何理由,你最喜歡的背景method.please指導我。 – 2011-07-30 14:11:22

相關問題