当文件被删除IOS时,performSelectorOnMainThread

时间:2013-08-29 21:17:50

标签: ios cocoa-touch nsfilemanager

我如何使用该方法:

[receiver performSelectorOnMainThread:@selector(sel) withObject:nil waitUntilDone:NO]

从路径删除文件时运行另一个方法?这就是它的完成方式:

NSFileManager *fileManager = [NSFileManager defaultManager];
                [fileManager removeItemAtPath:myFilePath error:NULL];

2 个答案:

答案 0 :(得分:1)

简单,removeItemAtPath:具有BOOL返回类型,这意味着if将返回YES或NO,具体取决于它是否成功,因此暗示它已完成写入。话虽如此,您可以将调用包装在if语句中,并从中调用performSelectorOnMainThread:。

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager removeItemAtPath:myFilePath error:nil]) {
    [receiver performSelectorOnMainThread:@selector(sel) withObject:nil waitUntilDone:NO]
}

来自the docs:

  

如果项目已成功删除或路径是,则返回值YES   零。如果发生错误,则返回NO。如果代表中止了   对于文件的操作,此方法返回YES。但是,如果   delegate中止目录的操作,此方法返回NO。

答案 1 :(得分:1)

尝试使用Grand Central Dispatch:

NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL didRemoveFile = [fileManager removeItemAtPath:myFilePath error:NULL];

dispatch_async(dispatch_get_main_queue(), ^{
    if (didRemoveFile) {
       // do something like inform everything went ok
    } else {
        // you could inform something bad happened here
    }

    // or perhaps you want a method to get called, every time
});