我们无法使用此KMP代码在iOS应用程序上成功写入数据. 我们想知道我们可以改变什么来使它成功地写入:
suspend fun HttpResponse.saveBodyToFile(fileSystem: FileSystem, outputFile: Path): Boolean {
var success = false
try {
// if the target file already exists, remove it.
fileSystem.delete(outputFile)
val parentDir = outputFile.parent ?: return false
if (!fileSystem.exists(parentDir)) {
fileSystem.createDirectories(parentDir)
}
val byteReadChannel: ByteReadChannel = body()
fileSystem.sink(outputFile).buffer().use {
byteReadChannel.readFully(it)
}
success = fileSystem.exists(outputFile)
} catch (e: KtorIoException) {
Logger.e(e) { "Failed to save response stream to ${outputFile.name}" }
Logger.e { "I/O Exception ${e.message}"}
fileSystem.delete(outputFile)
} catch (e: OkioIOException) {
Logger.e { "I/O Exception ${e.message}"}
Logger.e(e) { "Failed to save response stream to ${outputFile.name}" }
Logger.e { "I/O Exception ${e.message}"}
fileSystem.delete(outputFile)
}
return success
}
奇怪的是,它创建了一个目录,但抛出了这个错误: 🔴I/O异常只读文件系统
如果该目录已经存在,它仍然将其判断为True:
(!fileSystem.exists(parentDir))
并试图再次创造它.
如果该目录存在,并且这段代码被注释掉:
if (!fileSystem.exists(parentDir)) {
fileSystem.createDirectories(parentDir)
}
那么这段代码:
fileSystem.sink(outputFile).buffer().use {
byteReadChannel.readFully(it)
}
引发此错误: 🔴I/O异常只读文件系统
我们传入一个Cache目录的文件夹:
file:/Users/myname/Library/Developer/CoreSimulator/Devices/85FF4164-D3CF-446C-AF42-EC64B5473BCA/data/Containers/Data/Application/7F890ECD-83DB-4D91-8BBD-102F631594A6/Library/Caches/syncFiles/sync-proxy.json