NSFILEMANAGER CRASHING IN APP DELEGATE

Posted by theiphoneguy on Stack Overflow See other posts from Stack Overflow or by theiphoneguy
Published on 2010-04-28T02:46:43Z Indexed on 2010/04/28 2:53 UTC
Read the original article Hit count: 545

Filed under:
|
|
|
|

I have this code in a method called from applicationDidFinishLaunching. It works in the simulator, but crashes on the iPhone. There are about 1,600 2KB mp3 files being copied in this operation. If I try to instantiate the app multiple times, it will eventually copy more each time until the app eventually will start without crashing. I am releasing everything I allocate. I have about 20GB disk space free on the iPhone. If I progressively comment out code and run it on the iPhone, the copyItemAtPath seems to be the suspect.

  • (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 {

    } }

© Stack Overflow or respective owner

Related posts about nsfilemanager

Related posts about iphone