开发者

difference among NSthread and NStimer and NSNotifcation?

What is the difference among following codes

1)

 [NSThread detachNewThreadSele开发者_开发技巧ctor:@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)

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunction:) name:thename object:nil];


  1. Performs selector in a separate thread. You need to set up your own autorelease pool if you want to autorelease objects in the called function. You cannot directly work with GUI but you won’t block it if the function takes too long to finish. Probably better written using performSelectorInBackground: of NSObject.

  2. Runs selector after delay on the main thread. No need for an autorelease pool (you’re using the default one), you can work with GUI directly but you will block it if the function takes too long to finish.

  3. Very much like 2.

  4. Something completely different, see the documentation for NSNotificationCenter. You tell the default notification center that you want to receive all notification of name thename sent by nil (= any object). When such notification is posted, the notification center will call myFunction and pass it an instance of NSNotification that describes the event.

I hope I got everything right, the first three points are a bit tricky.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜