0

我在从 applicationDidFinishLaunching 调用的方法中有此代码。它在模拟器中工作,但在 iPhone 上崩溃。在此操作中复制了大约 1,600 个 2KB 的 mp3 文件。如果我尝试多次实例化应用程序,它最终会每次复制更多,直到应用程序最终启动而不会崩溃。我正在释放我分配的一切。我在 iPhone 上有大约 20GB 的可用磁盘空间。如果我逐步注释掉代码并在 iPhone 上运行它,copyItemAtPath 似乎是嫌疑犯。

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

    }
}
4

4 回答 4

1

如果在一定时间内无法启动,操作系统将终止您的应用程序。尝试在后台线程上进行复制。

于 2011-03-29T20:36:53.847 回答
0
NSFileManager *fileManager = [[NSFileManager alloc] init];

在黑暗中刺伤,但您是否尝试过使用NSFileManager的非线程安全版本:

NSFileManager* filemanager = [NSFileManager defaultManager];
于 2011-03-08T17:53:47.267 回答
0

由于您在循环中执行所有这些操作,因此自动释放池可能已满。手动刷新自动释放池或手动释放内存:

NSData data = [[NSData alloc] initWithContentsOfFile:component];
[data writeToFile:writableAudioPath atomically:NO];
[data release];
于 2010-04-28T04:00:36.980 回答
0

您可能会被 iOS 看门狗杀死,任何启动时间过长的应用程序都会被终止。尝试调用该方法,performSelector:withObject:afterDelay: 以便它在您退出 didFinishLaunching 方法后运行。

于 2011-03-29T20:50:46.697 回答