0

When I use it like that the ns timer worksi.e its calling both foo1 and foo1

-(void)register1
{
    NSRunLoop * runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(foo) userInfo:nil repeats:YES] forMode:NSDefaultRunLoopMode];
     [runLoop addTimer:[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(foo2) userInfo:nil repeats:YES] forMode:NSDefaultRunLoopMode];
    [runLoop run];
}

but my requirement is I have to use nstimer in different functions so that I can create an nsoperation of both.The code below only calls the first function.i.e when I call both register1 and register2 from main only 1 timer is registered.the top one

-(void)register1
{
    NSRunLoop * runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(foo) userInfo:nil repeats:YES] forMode:NSDefaultRunLoopMode];

    [runLoop run];
}
-(void)register2
{

    NSRunLoop * runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(foo2) userInfo:nil repeats:YES] forMode:NSDefaultRunLoopMode];
    [runLoop run];
}

I have found an answer to my question,thought I should tell others too.The solution is I had to use different threads for each function.I used nsoperation in main in this way

abc *ab=[[abc alloc]init];
//[ab register1];
  //  [ab register2];


    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    NSInvocationOperation *abc=[[NSInvocationOperation alloc]initWithTarget:ab selector:@selector(register1) object:(nil)];
    NSInvocationOperation *abc2=[[NSInvocationOperation alloc]initWithTarget:ab selector:@selector(register2) object:(nil)];
   [queue addOperation:abc];
    [queue addOperation:abc2];
zzzzz
  • 1,209
  • 2
  • 18
  • 45
  • Have you tried increasing the scope of runLoop? I'd be afraid that it is getting disposed of before you are finished using it. – David Brunow Nov 29 '12 at 06:38

2 Answers2

1
  1. Don't call -[NSRunLoop addTimer:forMode:] when you're using +[NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:] - it's redundant. The scheduled part of the name says it all: it's creating a timer that's already scheduled on the current runloop for you.
  2. Don't call -[NSRunLoop run], in general. It runs the runloop indefinitely. It basically never returns. So your second function isn't actually being called, since you never return from your first one. You could confirm this trivially by inserting NSLog statements in various places, or stepping through your code in the debugger.

If you post more of your code - particularly your main() function - then we can provide you further answers. Generally you want to call -[NSRunLoop run] (or variations thereof) only from main().

Wade Tregaskis
  • 1,996
  • 11
  • 15
0

you can get the NSTimer's point address like this

NSTimer NSTimer * aTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(foo) userInfo:nil repeats:YES] forMode:NSDefaultRunLoopMode];

and use it to another method.

ryuikuya
  • 75
  • 2
  • 7