I have a function that appends data to a file by calling the following extension:
extension Data {
func append(fileURL: URL) throws {
if let fileHandle = FileHandle(forWritingAtPath: fileURL.path) {
defer {
fileHandle.closeFile()
}
fileHandle.seekToEndOfFile()
fileHandle.write(self)
}
else {
try write(to: fileURL, options: .atomic)
}
}
}
func appendToFile(text:String, url:URL) {
let data = text.data(using: String.Encoding.utf8)!
try data.append(fileURL: url)
}
Now this function is called multiple times within DispatchQueue.global(qos: .userInitiated).async {...} Could this be a problem if the function is called another time while it's still writing? If so, how do I make it thread-safe?