This article is reproduced to http://www.mgenware.com/blog/?p=4631. Cancellation of selector sources in the current run loop
PerformSelector:withObject:afterDelay in NSObject: The method will create a timer in the current thread's run loop based on the Afterdelay parameter, if there is no method called with the Inmodes parameter, The timer runs in the default mode of the current run loop, which is the pattern defined in Nsdefaultrunloopmode.
PerformSelector:withObject:afterDelay: The use of methods seems to be quite simple. Here is another auxiliary function, the static Cancelpreviousperformrequestswithtarget method in NSObject. This method is specifically used to cancel the PerformSelector:withObject:afterDelay: The selector source created by the method (inside is the timer source of a run loop). So this method is the same as the PerformSelector:withObject:afterDelay: method, and is limited to the current run loop.
We can use Cancelpreviousperformrequestswithtarget to cancel all the non-executed performSelector:withObject:afterDelay of an object directly in the current run loop: The selector Sources generated by the method, the following code:
-(void) viewdidload
{
[Super Viewdidload];
[Self performselector: @selector (test:) Withobject:nil afterdelay:1];
[Self performselector: @selector (test:) withobject:@ "MGen" afterdelay:2];
[NSObject cancelpreviousperformrequestswithtarget:self];
}
-(void) test: (ID) obj
{
NSLog (@ "Call succeeded:%@", obj);
}
There will be no output, because two calls are canceled.
If you want to cancel a single one, use the CancelPreviousPerformRequestsWithTarget:selector:object: method, note that the selector and the object parameter require one by one correspondence. The following code:
-(void) viewdidload
{
[Super Viewdidload];
[Self performselector: @selector (test:) Withobject:[nsnumber numberwithint:26] afterdelay:1];
[Self performselector: @selector (test:) Withobject:[nsnumber numberwithint:17] afterdelay:2];
[Self performselector: @selector (test:) Withobject:[nsnumber numberwithint:17] afterdelay:3];
[NSObject cancelpreviousperformrequestswithtarget:self selector: @selector (test:) Object:[nsnumber numberwithint:17 ]];
}
-(void) test: (ID) obj
{
NSLog (@ "Call succeeded:%@", obj);
}
will only output:
Call succeeded: 26
The other two selector have been canceled.
Return to Directory 2. Execute selector in Nsthread
This topic is very simple, directly through the NSObject Performselectorinbackground:withobject: Method can be, the following code:
-(void) viewdidload
{
[Super Viewdidload];
[Self threadinfo:@ "UI"];
[Self performselectorinbackground: @selector (test:) Withobject:nil];
}
-(void) test: (ID) obj
{
@autoreleasepool
{
[Self threadinfo:@ "test"];
}
}
-(void) Threadinfo: (nsstring*) Category
{
NSLog (@ "%@-%@", category, [Nsthread CurrentThread]);
}
Output:
UI-<nsthread:0x71639e0>{name = (null), num = 1}
Test-<nsthread:0x7176ad0>{name = (null), num = 3}
This method is completely equivalent to Nsthread's DetachNewThreadSelector:toTarget:withObject: Static method, then the performselectorinbackground above nsobject: Withobject: The method call can be replaced completely:
[Nsthread detachnewthreadselector: @selector (test:) totarget:self Withobject:nil];
Of course, the user can also manually create a nsthread to complete the above functions, the code is as follows:
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (test:) Object:nil];
[Thread start];
These two methods run after the output is the same as the first kind.
Return to Directory 3. Execute selector in run loop in Nsthread
The method needed here is the NSObject PerformSelector:onThread:withObject:waitUntilDone: method. Since the selector is executed in another nsthread, we need to start the run Loop manually. First, you need to define two fields in Viewcontroller, the flag that is executed by the Nsthread and the control line range run loop, respectively.
@interface Viewcontroller ()
{
Nsthread *_thread;
BOOL _isnewthreadaborted;
}
The next thing to do is to execute this thread, and manually invoke the Nsrunloop Runmode:beforedate: method in Threads. Note here that if the run loop does not have any source, the method returns immediately, so you need to create a loop to continuously invoke the runmode:beforedate of the Run Loop: method. And try to end the loop at the end of the selector execution. The final code is as follows:
-(void) viewdidload
{
[Super Viewdidload];
[Self threadinfo:@ "UI"];
_isnewthreadaborted = NO;
_thread = [[Nsthread alloc] initwithtarget:self selector: @selector (newthread:) Object:nil];
Start thread
[_thread start];
Executes the selector in the run loop in another thread
[Self performselector: @selector (test:) Onthread:_thread Withobject:nil Waituntildone:no];
}
Create and start a nsrunloop in a new thread
-(void) Newthread: (ID) obj
{
@autoreleasepool
{
Nsrunloop *currentrunloop = [Nsrunloop currentrunloop];
while (!_isnewthreadaborted)
{
[Currentrunloop Runmode:nsdefaultrunloopmode beforedate:[nsdate distantfuture];
}
NSLog (@ "thread Stop");
}
}
Selector execution
-(void) test: (ID) obj
{
[Self threadinfo:@ "test"];
_isnewthreadaborted = YES;
}
-(void) Threadinfo: (nsstring*) Category
{
NSLog (@ "%@-%@", category, [Nsthread CurrentThread]);
}
Output:
UI-<nsthread:0x717e7e0>{name = (null), num = 1}
Test-<nsthread:0x8078a80>{name = (null), num = 3}
Thread stop
Finally, note the PerformSelector:onThread:withObject:waitUntilDone: The last Waituntildone parameter in the method, if you pass Yes, The current thread waits for selector to continue execution after it finishes executing in another thread.
The relevant methods of executing selector in Ios:nsobject