【发布时间】:2019-09-30 11:42:12
【问题描述】:
我的应用程序(Swift 5、Xcode 10、iOS 12)的第一个视图有一个“用户名”TextField 和一个“登录名”Button。单击该按钮检查我的 FTP 服务器上是否有输入用户名的文件,并将其下载到设备上的Documents 文件夹。为此,我使用FileProvider。
我的代码:
private func download() {
print("start download") //Only called once!
let foldername = "myfolder"
let filename = "mytestfile.txf"
let server = "192.0.0.1"
let username = "testuser"
let password = "testpw"
let credential = URLCredential(user: username, password: password, persistence: .permanent)
let ftpProvider = FTPFileProvider(baseURL: server, mode: FTPFileProvider.Mode.passive, credential: credential, cache: URLCache())
ftpProvider?.delegate = self as FileProviderDelegate
let fileManager = FileManager.default
let source = "/\(foldername)/\(filename)"
let dest = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(filename)
let destPath = dest.path
if fileManager.fileExists(atPath: destPath) {
print("file already exists!")
do {
try fileManager.removeItem(atPath: destPath)
} catch {
print("error removing!") //TODO: Error
}
print("still exists: \(fileManager.fileExists(atPath: destPath))")
} else {
print("file doesn't already exist!")
}
let progress = ftpProvider?.copyItem(path: source, toLocalURL: dest, completionHandler: nil)
progressBar.observedProgress = progress
}
我正在检查该文件是否已存在于设备上,因为FileProvider 似乎没有提供用于下载的copyItem 函数,该函数还可以让您覆盖本地文件。
问题是copyItem 尝试做所有事情两次:第一次下载文件成功(它实际上存在于Documents,我检查了)因为如果文件已经存在,我手动删除它。第二次尝试失败,因为文件已经存在并且这个copyItem函数不知道如何覆盖,当然也不会调用我的代码再次删除原始文件。
我能做些什么来解决这个问题?
编辑/更新:
我在我的 ftp 服务器的根目录下创建了一个简单的“sample.txt”(里面的文本:“来自 sample.txt 的 Hello world!”),然后尝试只读取该文件以便以后自己保存。为此,我使用“Sample-iOS.swift”文件here中的这段代码。
ftpProvider?.contents(path: source, completionHandler: {
contents, error in
if let contents = contents {
print(String(data: contents, encoding: .utf8))
}
})
但它也会这样做两次! “sample.txt”文件的输出是:
Optional("Hello world from sample.txt!")
Fetching on sample.txt succeed.
Optional("Hello world from sample.txt!Hello world from sample.txt!")
Fetching on sample.txt succeed.
为什么它也调用了两次?我只调用我的函数一次,“开始下载”也只打印一次。
编辑/更新 2:
我做了更多调查,发现在 contents 函数中调用了两次:
- 这是整个
self.ftpDownload部分! - 在 FTPHelper.ftpLogin 中,整个
self.ftpRetrieve部分是 调用了两次。 - 在 FTPHelper.ftp 中检索整个
self.attributesOfItem部分被调用了两次。 - 可能还有……
ftpProvider?.copyItem 使用相同的ftpDownload 函数,所以至少我知道为什么contents() 和copyItem() 都会受到影响。
同样的问题仍然存在:为什么它调用这些函数两次,我该如何解决这个问题?
【问题讨论】:
标签: ios swift ftp fileprovider