开发者

NSTimer wont stop [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

NSTimer doesn't stop

I am having a hard time stopping my timer, witch pings to my server. I already searched for other answers here and on other places, but i can`t seem to find where i have gone wrong.

I decided to make an example code with the same idea, but, you click a button the timer starts, you click another the timer ends, and it worked the way it should. Please don't mind if i did something wrong (other than the timer part) i'm new in this. All i want to know is why won`t it stop..

Thanks in advance.

Connection.h

#import <Foundation/Foundation.h>


@interface Connection : NSObject 
{  
NSString *urlString;
NSURL *url;
NSMutableURLRequest *request;
NSURLConnection *connection;
NSURLResponse *response;
NSMutableData *receivedData;
NSData *responseData;
NSError *error;

NSTimer *timer;
}
@property (nonatomic, retain) NSTimer *timer;

-(BOOL)authenticateUser:(NSString *)userName Password:(NSString *)password;
-(BOOL)checkConnection;
-(void)ping:(NSTimer *)aTimer;
-(void)logout;
-(void)timerStart;
-(void)timerStop;

@end

Connection.m

#import "Connection.h"
#import "Parser.h"
#import "Reachab开发者_开发知识库ility.h"
#import "TBXML.h"

@implementation Connection

@synthesize timer;

-(BOOL) authenticateUser:(NSString *)userName Password:(NSString *)password
{
BOOL success;
urlString = [[NSString alloc] initWithFormat:@"my/server/address/login"];
url =[[NSURL alloc] initWithString:urlString];
request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
error = [[NSError alloc] init];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[responseData retain];
NSString *tempString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *tempDict= [[NSMutableDictionary alloc] init];
if (request)
{
    Parser *parser = [[Parser alloc] init];
    tempDict = [parser readXMLString:tempString];
    for (id key in tempDict)
    {
        NSLog(@"%@ is %@",key,[tempDict objectForKey:key]);
    }
    if ([[tempDict objectForKey:@"login"] isEqualToString:@"true"] )
    {
        success = YES;
            self.timerStart;
    }
    else
    {
        success = NO;
    }
}
[urlString release];
[url release];
[error release];
[responseData release];
[tempString release];
return success;
}

-(void)logout
{
    self.timerStop;
}

-(void)ping:(NSTimer *)aTimer;
{
urlString = [[NSString alloc] initWithFormat:@"my/server/address"];
url =[[NSURL alloc] initWithString:urlString];
request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];

NSLog(@"ping");

[urlString release];
[url release];
}

-(BOOL)checkConnection
{
Reachability *reachability = [Reachability reachabilityWithHostName:@"http://my/server/address"];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];

if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
    return NO;
}
else
{
    return YES;
}
}

-(void)timerStart
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ping:) userInfo:nil repeats:YES];
}
-(void)timerStop
{
[self.timer invalidate];
self.timer = nil;
}


@end


In timerStart you just replace whatever is in the timer property. If you start a second timer without stopping the first one, it will run forever. So timerStart should first call timerStop before creating a new one (and should probably have a new name then as it would be silly to call timerStop from timerStart).


Use [self timerStop]; using dot syntax is ONLY for properties (and will generate a warning if you don't), not calling a method in the way you're doing it.

Edit: This won't fix your problem, but doing it the way you are is very bad coding practice


-(void)timerStart
{
    [self.timer invalidate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ping:) userInfo:nil repeats:YES];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜