Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSFileManager.removeItemAtPath是否异步?_Ios_Swift_Uitableview_Nsfilemanager_Synchronous - Fatal编程技术网

Ios NSFileManager.removeItemAtPath是否异步?

Ios NSFileManager.removeItemAtPath是否异步?,ios,swift,uitableview,nsfilemanager,synchronous,Ios,Swift,Uitableview,Nsfilemanager,Synchronous,在文档中没有关于这方面的内容,似乎该方法应该是同步的,它甚至返回一个成功值。 我有一个包含一些单元格的UITableView,用户可以删除与这些单元格关联的项目。这将调用以下代码: if(manager.fileExistsAtPath(vidUrl.path!)) { do { try manager.removeItemAtURL(vidUrl) } catch { NSLog("failed to delete vi

在文档中没有关于这方面的内容,似乎该方法应该是同步的,它甚至返回一个成功值。 我有一个包含一些单元格的UITableView,用户可以删除与这些单元格关联的项目。这将调用以下代码:

if(manager.fileExistsAtPath(vidUrl.path!))
{
    do
    {
        try manager.removeItemAtURL(vidUrl)
    }
    catch 
    {
        NSLog("failed to delete video at path \(vidUrl)")
    }
}
// reload the table data here
但每次调用时,当重新加载表数据时,文件仍然存在。我看不到任何方法可以将完成块传递给文件管理器,以便在文件删除时执行。NSFileManager委托只有“应该”方法,但没有“did”方法

我最后在catch语句之后添加了这些非常粗糙的行,这些行是有效的,但是肯定有更好的方法来处理这个问题吗

while(manager.fileExistsAtPath(vidUrl.path!))
{
    usleep(5000)
}
编辑 完成下面的代码(这由UIAlertAction触发)

还有loadVideos功能

func loadVideos()
{
    self.videos.removeAll()
    let documentsUrl = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
    do {
        let width = self.collectionView!.bounds.width * 0.4
        let targetSize = CGSizeMake(width,width)
        let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( documentsUrl, includingPropertiesForKeys: nil, options: [])
        let vidFiles = directoryContents.filter{ $0.pathExtension == VIDEO_EXTENSION }
        let validUrls = NSMutableArray()
        for vid in vidFiles
        {
            let thumbDuration = GalleryThumbLoader.getVideoThumb(vid.absoluteURL, size: targetSize)
            let video = VideoItem(filePath: vid.absoluteURL, bitmap: thumbDuration.bitmap, duration: thumbDuration.duration)
            videos.append(video)
            dispatch_async(dispatch_get_main_queue(),
            {
                self.collectionView!.reloadData()
            })
            validUrls.addObject(vid.absoluteURL.path!)
        }
    } catch let error as NSError {
        NSLog(error.localizedDescription)
        videos = [VideoItem]()
    }
    NSLog("finished loading videos")
}

你在子线程上完成它

在处理文件之前,您将对其进行判断。如下所示:
[NSThread isMainThread]

您可以在子线程上执行此操作

在处理文件之前,您将对其进行判断。如下所示:
[NSThread isMainThread]

你能发布完整的代码吗?
reloadData()
在重复循环中非常昂贵。放在重复循环之后。好,但这与报告的问题无关。问题是vidFiles包含我刚刚删除的文件,如果我有一段时间没有睡觉,为什么你不从
self.videos
中删除这些项目呢?这比检索整个目录便宜吗?那么validurs的作用是什么呢?它实际上没有被使用。我怀疑它也是异步的,我的单元测试因为假设它是同步的而变得混乱。你有没有找到任何正式的答案或确认?你能发布完整的代码吗?
reloadData()
在重复循环中是非常昂贵的。放在重复循环之后。好,但这与报告的问题无关。问题是vidFiles包含我刚刚删除的文件,如果我有一段时间没有睡觉,为什么你不从
self.videos
中删除这些项目呢?这比检索整个目录便宜吗?那么validurs的作用是什么呢?它实际上没有被使用。我怀疑它也是异步的,我的单元测试因为假设它是同步的而变得混乱。你有没有找到任何官方的答复或确认?
func loadVideos()
{
    self.videos.removeAll()
    let documentsUrl = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
    do {
        let width = self.collectionView!.bounds.width * 0.4
        let targetSize = CGSizeMake(width,width)
        let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( documentsUrl, includingPropertiesForKeys: nil, options: [])
        let vidFiles = directoryContents.filter{ $0.pathExtension == VIDEO_EXTENSION }
        let validUrls = NSMutableArray()
        for vid in vidFiles
        {
            let thumbDuration = GalleryThumbLoader.getVideoThumb(vid.absoluteURL, size: targetSize)
            let video = VideoItem(filePath: vid.absoluteURL, bitmap: thumbDuration.bitmap, duration: thumbDuration.duration)
            videos.append(video)
            dispatch_async(dispatch_get_main_queue(),
            {
                self.collectionView!.reloadData()
            })
            validUrls.addObject(vid.absoluteURL.path!)
        }
    } catch let error as NSError {
        NSLog(error.localizedDescription)
        videos = [VideoItem]()
    }
    NSLog("finished loading videos")
}