2011-08-23 82 views
1

可能重複:
NSTimer doesn't stop的NSTimer不會停止

我有一個很難停止我的定時器,巫坪到我的服務器。 我已經在這裏和其他地方搜索了其他答案,但我似乎無法找到我出錯的地方。

我決定用同樣的想法的示例代碼,但是,你點擊一個按鈕計時器開始,單擊其他計時器結束時,和它的工作的方式應該。請不要介意我做錯了什麼(除了定時器部分)我是新手。所有我想知道的是爲什麼那倒也停止..提前

感謝。

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 

回答

2

在timerStart您只需更換無論是在計時器屬性。如果你啓動第二個計時器而不停止第一個計時器,它將永遠運行。所以timerStart應該在創建一個新的之前先調用timerStop(並且應該有一個新的名字,然後從timerStart調用timerStop會很愚蠢)。

+0

但我不是創造一個新的,至少從我的理解。 當用戶登錄時,身份驗證方法啓動計時器。 當用戶離開屏幕/註銷時,方法viewDidUnload將調用註銷,這將調用timerStop,終止我創建的最後(和唯一)計時器,我是嗎?除非你說通過設置self.timer屬性,我實際上是在創建一個新的屬性? – Erakk

+0

好的,我發現罪魁禍首: 在我的RootViewController中,我正在實例化我的Connection來登錄,但是在我釋放它的同一個線程中。這個線程創建了計時器。 但是,當我離開視圖時,我需要銷燬定時器,所以我創建了另一個Connection實例,只是爲了註銷,之後立即釋放它。所以你的建議適用於這裏,因爲我創造了一個時間,但摧毀了另一個不存在的。 – Erakk

0

使用[self timerStop];使用點語法是隻對屬性(如果你不這樣做會產生警告),而不是調用一個方法就像你在做的那樣。

編輯:這不會解決你的問題,但這樣做你是這樣的非常壞的編碼實踐

+0

也許你應該澄清的是變化,不會有所作爲。這可能是錯誤的編碼練習,但結果將完全相同。 –

+0

是的,好主意:) –

+0

謝謝,我正在學習目標C,因爲我寫這個,所以任何建設性的批評是讚賞。 – Erakk

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