我正在使用
try FileManager.default.copyItem(at: inFile, to: outFile)问题是,这是一个存在问题的网络上的一个大文件,由某个无人值守的设备进行处理。我希望能够告诉FileManager只需要一分钟的时间来完成复制,否则就不会。
我似乎在文档中找不到它,而且我的冰福今天很弱。
发布于 2021-02-11 06:07:25
最后,我使用dispatchWorkItem和自定义错误结构扩展了FileManager。
public extension FileManager {
struct TimeoutError:Error{
let source:URL
let destination:URL
let timeOut:Double
}
func timedOutCopy(at source:URL, to destionation:URL, timeOut:Double = 15.0) throws {
var cpError:Error?
let d = DispatchWorkItem(block: {
do {
try self.copyItem(at: source, to: destionation)
} catch {
cpError = error
}
})
DispatchQueue.global().async(execute: d)
if d.wait(wallTimeout: DispatchWallTime.now() + timeOut) != .success {
d.cancel()
throw TimeoutError(source: source, destination: destionation, timeOut: timeOut)
} else {
if let cpError = cpError {throw cpError}
}
}
}https://stackoverflow.com/questions/66125357
复制相似问题