ios - iOS:刷新所有输出文件
问题描述
是否有一种 iOS 方法来刷新所有打开的输出文件——这样当调用返回时,所有待处理的缓冲数据都被写入持久存储?我的意思是笼统地说——我无权访问特定的文件句柄。我已经尝试过sync()
Swift(一个 POSIX 调用),但这似乎不能保证在数据返回之前真正确保数据到达持久存储(参见http://pubs.opengroup.org/onlinepubs/009696899/functions/sync.html) .
解决方案
不确定这是否能解决一般问题,但它解决了我的需求。这就是我正在做的事情:
// Write to file (in my case, this is a log file where I don't have access to the file handle).
// I'm using logging to a file with SwiftyBeaver. https://github.com/SwiftyBeaver/SwiftyBeaver
DispatchQueue.main.async {
// Read from the file-- it has the flushed contents. I'm doing:
do {
let fileContents = try String(contentsOfFile: path)
} catch (let error) {
}
}
看起来文件在事件循环结束时被刷新。
推荐阅读
- macos - AppleScript 将日期附加到文件
- collatz - collatz 序列 - 尝试除外;浮点数输入
- delphi - 使用 RAD 服务器更改查询中的数据库时出现 EMS 错误
- react-native - 键盘避免多个文本输入
- quarkus - Quarkus 应用程序使用授权码授予类型连接到服务器
- javascript - Is it possible to distinguish cookies with the same name by domain?
- css - CCS Code to remove title header on content box
- java - 为什么我的 JRE 位于 C:\Program Files (x86) 中,尽管它是 64 位版本?
- powershell - 使用 PIV 卡将网络共享中的文件夹重命名为其他用户的脚本
- kubectl - Kubectl 同时应用和设置镜像