?
Solved

nstime in for statement

Posted on 2011-09-25
1
Medium Priority
?
790 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

The Basics of Storyboard Storyboards were first introduced to developers in the release of iOS 5. They save the developer the trouble of designing each screen’s interface in a different file. The storyboard allows you to see the conceptual mockup o…
The release of the iPhone 6 and the iPhone 6 Plus was extremely successful for Apple. However when iOS 8.1 was introduced, many iPhone users complained of quick battery drainage. Without even knowing, you may be unnecessarily using up your battery p…
The goal of this video is to provide viewers with basic examples to understand opening and writing to 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.
Suggested Courses

752 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