2012-12-07 2 views
0

으로 사라지면 http://www.nhara.org/scored_races-2013.htm에서 정보를 수집하는 NSMutableData 변수가 있습니다. 약 90810 바이트가 포함되어있을 때 웹 사이트에서 정보를 얻은 후 약 세 번째 시간이 지나면 NSString을 인쇄하면 null이기 때문에 사라지거나 null이됩니다. 여기 내 NSMutableData 데이터를 저장한다는 것입니다 가장 나를 혼란 어떤 코드내 프로그램에서 NSMutableData가

- (void)viewWillAppear:(BOOL)animated 
{ 
    // Create a new data container for the stuff that comes back from the service 
    xmlData = [[NSMutableData alloc] initWithCapacity:180000]; 

    [self fetchEntries]; 
    [super viewWillAppear:animated]; 
} 
- (void)fetchEntries 
{ 
     // Construct a URL that will ask the service for what you want 
    NSURL *url = [NSURL URLWithString: @"http://www.nhara.org/scored_races-2013.htm"];// 

    // Put that URL into an NSURLRequest 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 

    // Create a connection that will exchange this request for data from the URL 
    connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; 
} 

- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data 
{ 
    // Add the incoming chunk of data to the container we are keeping 
    // The data always comes in the correct order 
    [xmlData appendData:data]; 

    NSLog(@"%@",xmlData); 
    NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]autorelease]; 
    NSLog(@"xmlCheck = %@", xmlCheck); 

} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"error= %@",error); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)conn { 

    // We are just checking to make sure we are getting the XML 
    NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease]; 
    NSLog(@"xmlCheck2 = %@", xmlCheck); 

} 

이지만, 바이트 수를 동일하게 할 것을 주장하면서 다음을 잃는다.

NSMutableData의 크기에 제약이 있습니까? 아니면 단지 메모리 관리에 문제가 있습니까?

+0

ARC를 사용하고있는 것으로 추측합니다. 변수가 dealloc되었습니다. 강력한 속성을 가진 @property로 선언하십시오. – Rog

+0

@Rog 그는 ARC를 사용하지 않습니다.'autorelease'에 대한 호출을보세요? – rmaddy

+0

어디서 문제가 보이나요? 'didReceiveData' 메소드 또는'didFinishLoading' 메소드에서? 부분 데이터 ('didReceiveData')를 변환하면 부분 데이터가 유효한 UTF8 문자열이 아니기 때문에'nil' 문자열이 생길 수 있습니다. – rmaddy

답변

1

xmlData 변수에 대한 속성을 만들어야합니다. 당신은 ARC를 사용하는 경우 @interface MyClass 후 헤더 파일에서 당신은 유지하는 강력한 변경 ARC 이하로 사용하는 경우 당신은 강한두고 이렇게

@property (nonatomic, retain) NSMutableData * xmlData; 

같은 일을합니다. 변수를 사용하고자 할 때 self.xmlData

+0

왜 재산이 필요하다고 생각하십니까? 그렇지 않습니다. 게다가, 이것이 문제라면, 왜 이것이 처음 몇 시간 동안 작동할까요? – rmaddy

+0

내가 틀렸다고 정정 해주세요. 그러나 메소드에 설정된 변수를 메소드의 범위 밖에서 사용할 수 없다는 인상하에있었습니다. 그는'viewWillAppear'에서'xmlData'를 변수로 명시 적으로 설정하고 있습니다. 속성을 만들지 않으면 다른 방법으로 해당 변수에 어떻게 액세스 할 수 있습니까? –

+0

아니요,'xmlData'는'viewWillAppear :'에 변수의 선언이 없으므로'viewWillAppear :'에 대한 지역 변수가 아닙니다. 따라서이 변수는 인스턴스 변수 (또는 전역 변수이지만 가능성이 낮음) 여야합니다. – rmaddy