- [NSFileManager removeItemAtPath:error:]使应用程序崩溃

时间:2011-10-13 12:04:11

标签: iphone ios xcode nsfilemanager

尝试在启动时删除文件夹时出现错误。我们正在删除放置在用于存储缓存数据的文档中的文件夹。知道它崩溃的原因吗?

0   libsystem_kernel.dylib  0x364b62d0 __unlink + 8
1   libsystem_kernel.dylib  0x364b46a6 unlink + 2
2   libremovefile.dylib     0x313099c8 __removefile_process_file + 232
3   libremovefile.dylib     0x31309a50 __removefile_tree_walker + 100
4   libremovefile.dylib     0x313090f4 removefile + 88
5   Foundation              0x3156c11a -[NSFilesystemItemRemoveOperation main] + 138
6   Foundation              0x31551d14 -[__NSOperationInternal start] + 652
7   Foundation              0x31551a78 -[NSOperation start] + 16
8   Foundation              0x3156bfda -[NSFileManager removeItemAtPath:error:] + 46
9   VG                      0x0004f030 +[VGFileManager removeOldCacheFolder] + 319536
10  VG                      0x00003e1c -[VGAppDelegate application:didFinishLaunchingWithOptions:] + 11804
11  UIKit                   0x31de481a -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 766
12  UIKit                   0x31ddeb5e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 266
13  UIKit                   0x31db37d0 -[UIApplication handleEvent:withNewEvent:] + 1108
14  UIKit                   0x31db320e -[UIApplication sendEvent:] + 38
15  UIKit                   0x31db2c4c _UIApplicationHandleEvent + 5084
16  GraphicsServices        0x34720e70 PurpleEventCallback + 660
17  CoreFoundation          0x30ba5a90 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 20
18  CoreFoundation          0x30ba7838 __CFRunLoopDoSource1 + 160
19  CoreFoundation          0x30ba8606 __CFRunLoopRun + 514
20  CoreFoundation          0x30b38ebc CFRunLoopRunSpecific + 224
21  CoreFoundation          0x30b38dc4 CFRunLoopRunInMode + 52
22  UIKit                   0x31dddd42 -[UIApplication _run] + 366
23  UIKit                   0x31ddb800 UIApplicationMain + 664
24  VG                      0x0000bd96 0x1000 + 44438
25  VG                      0x00003990 0x1000 + 10640

这是在“9”上调用的函数。

+ (void)removeOldCacheFolder {

    BOOL oldCacheFolderRemoved = [[NSUserDefaults standardUserDefaults] boolForKey:kVGFileCacheFolder_1_2];

    if(!oldCacheFolderRemoved){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; //Get documents folder  
        NSString *cacheDirectory = [documentsDirectory stringByAppendingPathComponent:kVGFileCacheFolder_1_2]; 

        if ([[NSFileManager defaultManager] fileExistsAtPath:cacheDirectory]) {
            NSError *error = nil;

            if (!([[NSFileManager defaultManager] removeItemAtPath:cacheDirectory error:&error])) {
                ELog(@"Unresolved error %@, %@", error, [error userInfo]);
            }
            else{
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kVGFileCacheFolder_1_2];
            }
        }
    }

    BOOL oldSQLFileDeleted = [[NSUserDefaults standardUserDefaults] boolForKey:kVGFileDb_1_3];

    if(!oldSQLFileDeleted){

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; //Get documents folder

        NSString *sqlFile = [documentsDirectory stringByAppendingPathComponent:kVGFileDb_1_3]; 

        if ([[NSFileManager defaultManager] fileExistsAtPath:sqlFile]) {
            NSError *error = nil;

            if (!([[NSFileManager defaultManager] removeItemAtPath:sqlFile error:&error])) {
                ELog(@"Unresolved error %@, %@", error, [error userInfo]);
            }
            else{
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kVGFileDb_1_3];
            }
        }
    }
}
编辑:刚刚发现此过程可能需要2分钟......

2 个答案:

答案 0 :(得分:2)

也许我们被iOS监视器杀死,任何需要太长时间才能启动的应用都会被终止。这可能是一个太耗时的过程吗?

答案 1 :(得分:1)

iOS维护了一个特殊的缓存文件夹 - 它的内容不会备份,而您的数据将在同步时备份。

NSString *tmpDir = NSTemporaryDirectory();

您从转储中删除了异常类型。您也许可以从异常类型和代码中了解崩溃。

我仔细阅读了代码的1.2版本,老实说它看起来很好。检查您对kVGFileCacheFolder_1_2的定义 - 我假设像@“file.dat”这样的东西也很好。

检查文件是否已关闭,文件上没有其他正在进行的操作。