So, I'm trying to write a function to detect if a video file is currently being recorded to. The purpose is if it is, than you can't delete the file or rename it. Here's what I have:
public func fileSizeDetection(url: URL) -> Int{
let tempFile = URL(string:(try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).absoluteString) + "tempFile")
let keys : Set<URLResourceKey> = [URLResourceKey.fileSizeKey]
do{
try FileManager.default.copyItem(at: url, to: tempFile!)
usleep(5000)
}
catch{
print (error.localizedDescription)
}
let tempValues = try! tempFile!.resourceValues(forKeys: keys)
let tempSize = tempValues.fileSize!
let permValues = try! url.resourceValues(forKeys: keys)
let permSize = permValues.fileSize!
let difference = permSize - tempSize
do{
try FileManager.default.removeItem(at: tempFile!)
}
catch{
print (error.localizedDescription)
}
return difference
}
So, I it starts by passing the URL to test. Than it creates a temporary copy of the file which is supposed to be a snapshot of it. Than it sleeps for a second, and compares the current file size to the copied file size. If there's no difference than it's not a recording in progress. Finally it deletes the temp copy.
The issue I'm having, is that tempFile and the original URL are the same size! I tried to increase usleep, but it still doesn't work. Can anyone please tell me what I'm doing wrong!