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];
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:
ofNSObject
.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.
Very much like 2.
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 bynil
(= any object). When such notification is posted, the notification center will callmyFunction
and pass it an instance ofNSNotification
that describes the event.
I hope I got everything right, the first three points are a bit tricky.
精彩评论