首页 > 解决方案 > iOS:刷新所有输出文件

问题描述

是否有一种 iOS 方法来刷新所有打开的输出文件——这样当调用返回时,所有待处理的缓冲数据都被写入持久存储?我的意思是笼统地说——我无权访问特定的文件句柄。我已经尝试过sync()Swift(一个 POSIX 调用),但这似乎不能保证在数据返回之前真正确保数据到达持久存储(参见http://pubs.opengroup.org/onlinepubs/009696899/functions/sync.html) .

标签: iosfile

解决方案


不确定这是否能解决一般问题,但它解决了我的需求。这就是我正在做的事情:

// 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) {
    }
}

看起来文件在事件循环结束时被刷新。


推荐阅读