I used the code below to wait for NSTimer triggered
-(void)timer1Fired :(NSTimer *) timer
{
isTriggered=true;
}
//----------------------------------------------
isTriggered=false;
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timer1Fired:)
userInfo:nil
repeats:NO];
int j1 = 0;
while ( !isTriggered && j1 < 1000)
{
[NSThread sleepForTimeInterval: 0.5];
j1++;
}
//continue to do something //b
but it looks like NSTimer never be triggered
Welcome any comment
I don’t think
[NSThread sleepFortimeInterval]does what you expect it to do. You’re right,isTriggered:will not be executed until after your 500 second long while loop has finished.Also,
NSTimerandNSThreadare both old fashioned and you should be using Grand Central Dispatch instead (to replace both of them). GCD is significantly faster and uses less resources, and it is also more reliable/powerful.https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
If you want to avoid using a low level C API, you can use
NSOperationandNSOperationQueue, which are high level wrappers for GCD.