2010-04-28 75 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方法后运行该方法。