Solved

nstime in for statement

Posted on 2011-09-25
1
784 Views
Last Modified: 2013-11-25
i want to call a method in per 5 seconds. also there is a count and after 20 seconds if the count is equal to 4 then do something..like below code..

but I see, it writes NSLog(@"denemelik"); at every 5 second not after if cnt is 5. I think it must write the "denemelik" after 25 seconds because of if (cnt == 5 )

I hope you understand what I mean..Can you help me? How can I solve this problem?
for (int i=1; i<6; i++) {
    [NSTimer scheduledTimerWithTimeInterval: 5.0
                                     target: self
                                   selector:@selector(onTick:)
                                   userInfo: nil repeats:YES];
    cnt=i;

}


-(void)onTick:(NSTimer *)timer {
    if(timer)
{
    CLLocation *d =[[CLLocation alloc] init];
    ws6=[[GetData5 alloc]init];
    [ws6 GetWebservice5:@"123abc":d.coordinate.latitude:d.coordinate.longitude:0 :0:@"mesafe" ];
    NSMutableArray *arr = [[NSMutableArray alloc] init];        
    if (cnt == 5 ) 
        NSLog(@"denemelik");
    }
}
}

Open in new window

0
Comment
Question by:Kekosavar
1 Comment
 
LVL 4

Accepted Solution

by:
jacekmalinowski earned 500 total points
Comment Utility
Hi,

I guess the reason is the for (int i=1; i<6; i++).

1. You start 5 timers instead of 1.
2. cnt = 5 at start because scheduledTimerWithTimeInterval makes other threads which work asynchronously that is why for goes through 5 loops at start
3. Then onTick fires and cnt = 5 so it writes NSLog(@"denemelik");

You could try something like this:
cnt = 0;

[NSTimer scheduledTimerWithTimeInterval: 5.0
                                     target: self
                                   selector:@selector(onTick:)
                                   userInfo: nil repeats:YES];


-(void)onTick:(NSTimer *)timer {
    if(timer)
{
    cnt++; // increase cnt here
    CLLocation *d =[[CLLocation alloc] init];
    ws6=[[GetData5 alloc]init];
    [ws6 GetWebservice5:@"123abc":d.coordinate.latitude:d.coordinate.longitude:0 :0:@"mesafe" ];
    NSMutableArray *arr = [[NSMutableArray alloc] init];        
    if (cnt == 5 ) 
        NSLog(@"denemelik");
    }
}

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
There is a security feature on iOS devices that is nearly impenetrable when it has been activated.  This article will provide some possible solutions as well as necessary steps to take to ensure you do not end up with a locked device.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now