iphone - -[NSFileManager removeItemAtPath :error:] makes app CRASH

标签 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 分钟...

最佳答案

也许我们正在被 iOS 看门狗杀死,任何启动时间过长的应用程序都会被终止。这会不会是一个太耗时的过程?

关于iphone - -[NSFileManager removeItemAtPath :error:] makes app CRASH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7753838/

相关文章:

iphone - 如何将 Interface Builder 元素连接到 Xcode 变量?

iphone - 将 UIVIewController 推送到 UIView

ios - 使用 AFNetworking、ios 上传图片

iphone - 如何在 Xcode 中以编程方式更改 UITableViewController 的样式

ios - 即使其他功能有效,排行榜分数也不会显示在 GameCenter 中

ios - 将 UIButton 添加到 UICollectionViewCell 时的奇怪行为

xCode4 Lua 语法高亮

ios - 为什么我的模拟会以编程方式获得与 anchor /约束有关的空白黑屏?

iphone - 简单,没有多余的装饰,在 iPhone 中获取 LAT/LON

iphone - 如何在 iphone 的 NSString 中删除 ("") 双引号字符?