2013-03-15 3 views
2

다음 코드를 사용하여 서버에서 많은 파일을 다운로드합니다. 이 파일 중 일부는 비디오 파일 (> 60Mo)입니다.많은 큰 파일을 다운로드 할 때 iOS 메모리 경고가 발생했습니다.

이 함수는 루프에서 호출됩니다. 그것은 작은 파일을 완벽하게 작동합니다 ...

큰 파일을 너무 많이 다운로드하면 (의존합니다) 메모리 경고가 발생하고 응용 프로그램이 다운됩니다.

참고 : 프로젝트 내 응용 프로그램 위임에

- (bool) copyWebFile:(NSString *)url toFile:(NSString *)toFile ; 
{ 
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]] ; 
    if (data) 
    { 
    NSError *error ; 
    if ([[NSFileManager defaultManager] fileExistsAtPath:toFile]) 
    { 
     NSLog(@"Existant %@", toFile) ; 
     [[NSFileManager defaultManager] removeItemAtPath:toFile error:nil] ; 
    } 
    if ([data writeToFile:toFile options:NSDataWritingAtomic error:&error]==NO) 
    { 
     NSLog(@"@Error creating file-%@ \n", toFile) ; 
     NSLog(@"@Error description-%@ \n", [error localizedDescription]) ; 
     NSLog(@"@Error suggestion-%@ \n", [error localizedRecoverySuggestion]) ; 
     NSLog(@"Error reason-%@", [error localizedFailureReason]) ; 
    } 
    else 
    { 
     return(true) ; 
    } 
    } 
    return(false) ; 
} 

ARC

, 나는이 코드를 추가하지 : 차이.

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application 
{ 
    [[NSURLCache sharedURLCache] removeAllCachedResponses] ; 
} 

답변

0

사용 @autoreleasepool이 같은 시도 ..

- (bool) copyWebFile:(NSString *)url toFile:(NSString *)toFile ; 
    { 
     @autoreleasepool 
     { 
      NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]] ; 
      if (data) 
      { 
       NSError *error ; 
       if ([[NSFileManager defaultManager] fileExistsAtPath:toFile]) 
       { 
        NSLog(@"Existant %@", toFile) ; 
        [[NSFileManager defaultManager] removeItemAtPath:toFile error:nil] ; 
       } 
       if ([data writeToFile:toFile options:NSDataWritingAtomic error:&error]==NO) 
       { 
        NSLog(@"@Error creating file-%@ \n", toFile) ; 
        NSLog(@"@Error description-%@ \n", [error localizedDescription]) ; 
        NSLog(@"@Error suggestion-%@ \n", [error localizedRecoverySuggestion]) ; 
        NSLog(@"Error reason-%@", [error localizedFailureReason]) ; 
       } 
       else 
       { 
        return(true) ; 
       } 
      } 
      return(false) ; 
     } 
    } 
+1

'@의 autoreleasepool'은 많은 소규모 또는 중간이있을 때 도움이됩니다

이 질문은 코드 예제와 함께 그렇게하는 방법 응답 크기가 큰 파일은 거대한 파일이 아닙니다. 1GB는 여전히 메모리에로드되고 앱이 다운됩니다. – coverback

관련 문제