0
在文檔中沒有任何內容,似乎該方法應該是同步的,它甚至會返回成功值。 我有一個UITableView與一些單元格,用戶可以刪除與這些單元格相關的項目。這需要下面的代碼:NSFileManager.removeItemAtPath是否異步?
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委託只有「should」方法,但沒有「did」方法。
我最終在catch語句後面添加了這些非常奇怪的行,但肯定有更好的方法來處理這個問題?下面
while(manager.fileExistsAtPath(vidUrl.path!))
{
usleep(5000)
}
編輯 完整代碼(這是由UIAlertAction觸發)
let okAction = UIAlertAction(title: ok, style: UIAlertActionStyle.Destructive, handler:{
(action: UIAlertAction) -> Void in
HUD.show(HUDContentType.LabeledProgress(title: deleteString, subtitle: "Deleting..."))
for item in self.selectedVideos
{
let vidUrl = (item as! VideoItem).filePath
if(manager.fileExistsAtPath(vidUrl.path!))
{
do
{
try manager.removeItemAtURL(vidUrl)
// If I remove this while loop then files still exist in the loadVideos below
while(manager.fileExistsAtPath(vidUrl.path!))
{
usleep(5000)
}
}
catch {NSLog("failed to delete video at path \(vidUrl)")}
}
}
self.loadVideos()
HUD.hide()
})
和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")
}
你可以發佈完整的代碼嗎? – user3608500
重複循環中的'reloadData()'非常昂貴。把它放在重複循環之後。 – vadian
好,但這與報告的問題無關。問題是vidFiles包含我剛刪除的文件,如果我不睡一會兒 – tishu