2010-04-28 80 views
0

我在從applicationDidFinishLaunching調用的方法中有此代碼。它在模擬器中工作,但在iPhone上崩潰。在這個操作中有大約1,600個2KB的mp3文件被複制。如果我嘗試多次實例化應用程序,它最終會每次複製更多內容,直到應用程序最終會在不崩潰的情況下啓動。我釋放我分配的所有東西。我在iPhone上有大約20GB的磁盤空間。如果我逐漸註釋掉代碼並在iPhone上運行它,則copyItemAtPath似乎是可疑的。NSFileManager崩潰在應用程序代理

- (void)createCopyOfAudioFiles:(BOOL)force { 

    @try { 

     NSError *error; 
     NSString *component; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSFileManager *fileManager = [[NSFileManager alloc] init]; 

     NSEnumerator *enumerator = [[[NSBundle mainBundle]pathsForResourcesOfType:@"mp3" inDirectory:nil] objectEnumerator]; 

     while ((component = [enumerator nextObject]) != nil) { 

      NSArray *temp = [component componentsSeparatedByString:@".app/"]; 
      NSString *file = [NSString stringWithFormat:@"%@", [temp objectAtIndex:1]]; 
      NSString *writableAudioPath = [documentsDirectory stringByAppendingPathComponent:file]; 

      BOOL success = [fileManager fileExistsAtPath:writableAudioPath]; 

      if (success && !force) { 
       continue; 
      } else if (success && force) { 
       success = [fileManager removeItemAtPath:writableAudioPath error:&error]; 
      } 

      success = [fileManager copyItemAtPath:component toPath:writableAudioPath error:&error]; 

      if (!success) { 
       @throw [NSException exceptionWithName:[error localizedDescription] reason:[error localizedFailureReason] userInfo:nil]; 
      } 

     } 

     [fileManager release]; 
    } 
    @catch (NSException *exception) { 
     NSLog(@"%@", exception); 
     @throw [NSException exceptionWithName:exception.name reason:exception.reason userInfo:nil]; 
    } 
    @finally { 

    } 
} 

回答

0

由於您在循環中執行此操作,autorelease池可能已經滿了。無論是手動刷新自動釋放池或手動釋放內存:

NSData data = [[NSData alloc] initWithContentsOfFile:component]; 
[data writeToFile:writableAudioPath atomically:NO]; 
[data release]; 
+0

謝謝您的回覆。我同意你的觀點並實施了一個自動釋放池。結果不幸的是相同的。然後我交換了NSData writeToFile的NSFileManager copyItemAtPath。儘管如此,結果是一樣的。加載時應用程序崩潰。 – user273565 2010-04-29 02:36:04

0
NSFileManager *fileManager = [[NSFileManager alloc] init]; 

在黑暗中刺,但你嘗試過使用的NSFileManager的線程安全的版本:

NSFileManager* filemanager = [NSFileManager defaultManager]; 
1

如果操作系統無法在一定時間內啓動,操作系統將會終止您的應用程序。嘗試在後臺線程上做你的副本。

0

您可能正在被iOS看門狗殺死,任何需要太長時間才能啓動的應用程序終止。嘗試使用performSelector:withObject:afterDelay: 調用該方法,以便在您退出didFinishLaunching方法後運行該方法。