Objective c 正在强制FileManager()的超时。copyItem(:41;

Objective c 正在强制FileManager()的超时。copyItem(:41;,objective-c,swift,macos,timeout,nsfilemanager,Objective C,Swift,Macos,Timeout,Nsfilemanager,我正在使用 try FileManager.default.copyItem(at: inFile, to: outFile) 问题是,它是一个位于可疑网络上的大文件,并且由无人值守的程序进行处理。我希望能够告诉FileManager只需要一分钟就可以完成拷贝,否则就不行了 我似乎在文档中找不到它,而且我的Bing fu今天很弱。我最终用dispatchWorkItem和自定义错误结构扩展了FileManager public extension FileManager { struc

我正在使用

try FileManager.default.copyItem(at: inFile, to: outFile)
问题是,它是一个位于可疑网络上的大文件,并且由无人值守的程序进行处理。我希望能够告诉FileManager只需要一分钟就可以完成拷贝,否则就不行了


我似乎在文档中找不到它,而且我的Bing fu今天很弱。

我最终用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}
        }
    }
}

也许有了这些要求,您应该编写自己的文件副本,以部分方式完成,然后您就可以拥有更多的控制权,例如提前退出。