NSTimer不会停止

发布于 2024-12-01 09:40:54 字数 3160 浏览 4 评论 0原文

可能的重复:
NSTimer 不会停止

我很难停止我的计时器,女巫 ping 到我的服务器。 我已经在这里和其他地方搜索了其他答案,但我似乎找不到我出错的地方。

我决定制作一个具有相同想法的示例代码,但是,您单击一个按钮计时器开始,您单击另一个按钮计时器结束,并且它按应有的方式工作。请不要介意我是否做错了什么(除了计时器部分),我是新手。我想知道的是为什么它不会停止......

提前致谢。

连接.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

连接.m

#import "Connection.h"
#import "Parser.h"
#import "Reachability.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

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 "Reachability.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山川志 2024-12-08 09:40:54

在timerStart 中,您只需替换timer 属性中的任何内容即可。如果您启动第二个计时器而不停止第一个计时器,它将永远运行。因此,timerStart 在创建新计时器之前应首先调用timerStop(并且可能应该有一个新名称,因为从timerStart 调用timerStop 是愚蠢的)。

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).

不一样的天空 2024-12-08 09:40:54

使用 [self timerStop]; 使用点语法仅适用于属性(如果不这样做,将会生成警告),而不是以您正在执行的方式调用方法。

编辑:这不会解决你的问题,但按照你的方式做是非常糟糕的编码实践

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

长亭外,古道边 2024-12-08 09:40:54
-(void)timerStart
{
    [self.timer invalidate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ping:) userInfo:nil repeats:YES];
}
-(void)timerStart
{
    [self.timer invalidate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ping:) userInfo:nil repeats:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文