【问题标题】:NSTimer wont stop [duplicate]NSTimer 不会停止 [重复]
【发布时间】:2011-08-23 00:28:44
【问题描述】:

可能重复:
NSTimer doesn't stop

我很难停止计时器,女巫 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

【问题讨论】:

    标签: xcode nstimer


    【解决方案1】:

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

    【讨论】:

    • 但是 im not creating a new one, at least from what i understand. When the user logs in, the authenticate method starts the timer. When the user leaves the screen/logs off, the method viewDidUnload will call logout wich will call timerStop, terminating the last (and the only) timer i created, am i right? Unless youre 说通过设置 self.timer 属性我实际上是在创建一个新属性?
    • 好的,我找到了罪魁祸首:在我的 RootViewController 中,我正在实例化我的 Connection 以登录,但在同一个线程中我正在释放它。该线程创建了计时器。但是,当我离开视图时,我需要销毁计时器,所以我创建了另一个 Connection 实例,只是为了注销,然后立即释放它。所以你的建议在这里应用得很好,因为我创造了一个时代,却摧毁了另一个不存在的时代。
    【解决方案2】:

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

    编辑:这不会解决您的问题,但是按照您的方式进行操作是非常糟糕的编码习惯

    【讨论】:

    • 也许你应该澄清一下,改变不会有什么不同。这可能是不好的编码习惯,但结果将完全相同。
    • 谢谢,我在写这篇文章时正在学习目标 C,所以任何建设性的批评都值得赞赏。
    【解决方案3】:
    -(void)timerStart
    {
        [self.timer invalidate];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ping:) userInfo:nil repeats:YES];
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多