2013-07-16 36 views
0

我已經使用這樣的timmer調用方法。應用程序崩潰,只要第二次使用NSTimer調用方法

timmer= [NSTimer scheduledTimerWithTimeInterval:5.0 
                target:self 
               selector:@selector(callWaiter) 
               userInfo:nil repeats:YES]; 

這裏是callWaiter

-(void)callWaiter 
{ 
@try 

    { 
     NSLog(@"current serverTime----1=%@",currentSeverTime); 
     NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@table_update?tag=update&admin=%@&table=%@&time=%@",baseUrl,adminId,@"Waitercall",[currentSeverTime stringByReplacingOccurrencesOfString:@" " withString:@"%20"]]]; 
     JsonParse5 *js5=[[JsonParse5 alloc] init]; 
     if([[js5 fetchData:[NSData dataWithContentsOfURL:url]] count]>0) 
     { 
      UIAlertView *grAlert=[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@%@",LSSTRING(@"Waiter call - please attend table #"),[[[js5 fetchData:[NSData dataWithContentsOfURL:url]] objectAtIndex:0] objectForKey:@"table_no"] ] message:nil delegate:self cancelButtonTitle:LSSTRING(@"OK") otherButtonTitles:nil]; 

      [grAlert show]; 
      grAlert.tag=1; 
      [grAlert release]; 


     } 
     else 
     { 
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
      // this is imporant - we set our input date format to match our input string 
      // if format doesn't match you'll get nil from your string, so be careful 
      [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
      NSDate *dateFromString = [[NSDate alloc] init]; 
      // voila! 
      dateFromString = [dateFormatter dateFromString:currentSeverTime]; 
      NSDate *correctDate = [NSDate dateWithTimeInterval:5.0 sinceDate:dateFromString]; 
      currentSeverTime=[dateFormatter stringFromDate:correctDate]; 

      NSLog(@"current serverTime----2=%@",currentSeverTime); 






     } 

    } 
    @catch (NSException *exception) { 
     NSLog(@"exception=%@",exception); 
    } 


} 

的代碼,它做工精細第一次,而在第二次崩潰前「當前serverTime ---- 1」的日誌。 我在currentSeverTime中賦值如下。

currentSeverTime = @「2013-07-16 07:15:50」;

請幫助我謝謝。

+0

什麼是崩潰時出現的實際錯誤? –

+0

它沒有給出任何異常,只是在callWaiter方法內的第一行之後崩潰。 – Suraj

+0

它也沒有給出任何日誌 – Suraj

回答

0

我想原因是,經過

currentSeverTime=[dateFormatter stringFromDate:correctDate]; 

currentSeverTime不被保留,而當到達方法的末尾currentSeverTime得到了釋放。並currentSeverTime成爲一個野生的參考。它在第二次使用時會導致EXC_BAD_ACCESS。

您可以將currentSeverTime作爲屬性來更正代碼。 在h文件:

@property (retain) NSString *currentSeverTime; 

在.m文件:

  1. 在dealloc方法:

    [_currentSeverTime釋放];

  2. 使用這種方式:

    self.currentSeverTime = [dateFormatter stringFromDate:correctDate];

希望它有幫助。

相關問題