Solved

nstime in for statement

Posted on 2011-09-25
1
785 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 opening and reading files 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.

867 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

12 Experts available now in Live!

Get 1:1 Help Now