Solved

nstime in for statement

Posted on 2011-09-25
1
789 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
jacekmalinowski earned 500 total points
ID: 36598038
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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ā€¦
How to take pictures with depth using iOS 10
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

728 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