I am trying to delete the Core Data generated sqlite3 file for testing purposes in tear down.
Core Data creates the file using the following path.
/Users/mehreen/Library/Developer/CoreSimulator/Devices/62BB2C13-6640-408E-B2B9-0247473E24B4/data/Containers/Data/Application/**8507624D-3A06-4EEC-9C0D-2AAF328E43B6**/Library/Application Support
In my integration test I have the following code to delete the sqlite file in tear down method.
static func rollbackDatabase() {
let fileManager = FileManager.default
let applicationSupportUrl = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first! as NSURL
let applicationSupportPath = applicationSupportUrl.path
do {
let fileNames = try fileManager.contentsOfDirectory(atPath: applicationSupportPath!)
for fileName in fileNames {
let filePath = "\(String(describing: applicationSupportPath))/\(fileName)"
try fileManager.removeItem(atPath: filePath)
}
} catch {
print(error.localizedDescription)
}
}
The problem is that it points to the path
/Users/mehreen/Library/Developer/CoreSimulator/Devices/62BB2C13-6640-408E-B2B9-0247473E24B4/data/Containers/Data/Application/**FEB3A60C-CDC0-4D59-AF67-6D1723C01B1D**/Library/Application Support
which is different from the original Core Data file path. So on that path there are no files to delete. How can I get the same path as used by Core Data to the sqlite files in my test environment.
Related
I need to create a simple zip file with files included without parent directory in swift.
I found a good solution from Robin Kunde https://recoursive.com/2021/02/25/create_zip_archive_using_only_foundation/ but the files have a parent directory in the zip file.
So when you unzip the files, there is a directory with the name e.g. "Documents".
I don't need real compression or other zip features. Our backend server checks the content and the folder must be removed.
I don't think, that this is even possible without a C-library :(
I tried to decode the ZIP-file to UTF-8, delete the folder names and save the data to a file. The file was corrupt.
let fm = FileManager.default
let baseDirectoryUrl = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
let data = try! Data(contentsOf: baseDirectoryUrl.appendingPathComponent("archive.zip"))
let str = String(decoding: data, as: UTF8.self)
let newstr = str.replacingOccurrences(of: "Documents/", with: "")
do {
try newstr.write(to: baseDirectoryUrl.appendingPathComponent("archive_without_parent_name.zip"), atomically: true, encoding: .utf8)
} catch {
print(error.localizedDescription)
}
I created a file in the document directory and for some reason when I try to delete it using the code below, it doesn't get deleted. The code doesn't throw any errors, but the file is still there
let fileManager = FileManager.default
if let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
let filePath = documentDirectory.appendingPathComponent("data.json")
do {
try fileManager.removeItem(atPath: filePath.path)
} catch let error as NSError {
print(error.localizedDescription)
}
}
I also tried to put it inside another folder and remove the folder but still the same problem.
This is the path where the file is stored :
Users/user1/Library/Developer/CoreSimulator/Devices/76AFDB69-75C8-464E-93F2-6ABF622068FD/data/Containers/Data/Application/7D268156-977A-4A3C-834B-6B13FA3DE76D/Documents/
you can try like below
if let filePath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("data.json"), fileManager.fileExists(atPath: filePath.path) {
do {
try fileManager.removeItem(atPath: filePath.path)
} catch let error as NSError {
print(error.localizedDescription)
}
}
The code doesn't throw any errors, but the file is still there
No, it isn't. The problem is with the way you are checking to see whether "the file is still there". You are looking on your computer for the file. But iOS files are sandboxed. At the time you are looking, the path where the file was is completely different from your claimed path:
Users/user1/Library/Developer/CoreSimulator/Devices/76AFDB69-75C8-464E-93F2-6ABF622068FD/data/Containers/Data/Application/7D268156-977A-4A3C-834B-6B13FA3DE76D/Documents/
Such paths are not permanent. They are meaningless and should not be used. The only way to know whether the file is still there is with more code from inside iOS, i.e. ask the FileManager. When you do, you will find that you are, indeed, deleting the file successfully.
So the app I'm making creates a file called "logfile" and I'm trying to send that file via Alamofire upload to a server. The file path printed in the console log is
/var/mobile/Containers/Data/Application/3BE13D78-3BF0-4880-A79A-27B488ED9EFE/Documents/logfile.txt
and the file path I can use to manually access the log created in the .xcappdata is
/AppData/Documents/logfile.txt
To access it, I'm using
let fileURL = Bundle.main.url(forResource: "", withExtension: "txt")
where inbetween the double quotes for "forResource", I've tried both file paths I listed in the previous paragraph as well as just the file name but I'm getting a nil value for file found for either. The file isn't recognized to be there, presumably because the file path I'm using is wrong as Alamofire is returning nil when trying to locate send the file. Anyone know the direct file path I'm supposed to use to be able to grab my file since the other two don't supposedly work? Thank you!
Use below code to get string data from text file to upload to server:
let fileName = "logfile"
let documentDirURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = documentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt")
print("FilePath: \(fileURL.path)")
var readString = "" // Used to store the file contents
do {
// Read the file contents
readString = try String(contentsOf: fileURL)
} catch let error as NSError {
print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
}
print("File Text: \(readString)") // Send 'readString' to server
If you're dynamically creating the file at runtime, it won't be in your app bundle so the Bundle class won't be able to find it. The directories you see are also dynamically-generated and not only platform-specific, but also device-specific, so you can't use the file paths directly. Instead, you'll have to ask for the proper directory at runtime from the FileManager class, like this:
guard let documents = FileManager.default.urls(for: .documentsDirectory, in: .userDomainMask).first else{
// This case will likely never happen, but forcing anything in Swift is bad
return
}
let logURL = URL(string: "logfile.txt", relativeTo: documents)
do{
let fileContents = String(contentsOf: logURL)
// Send your file to your sever here
catch{
// Handle any errors you might've encountered
}
Note that I'm guessing based on the paths you pasted in your answer you put it in your application's documents directory. That's a perfectly fine place to put this type of thing, but if I'm wrong and you put it in a different place, you'll have to modify this code to point to the right place
I am new to swift and trying to save the file on iphone and view them using file manager app present in app store. but every time the path looks like its getting stored in my mac machine. below is code which i have written for storing a simple text file
func saveImageDocumentDirectory(){
let str = "Super long string here"
let filename = getDocumentsDirectory().appendingPathComponent("output.txt")
do {
try str.write(to: filename, atomically: true, encoding: String.Encoding.utf8)
print(filename.path)
} catch {
// failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding
}
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
but path at which file is getting stored is printed as below
/var/mobile/Containers/Data/Application/ACBC0B24-XXXX-XXXX-XXXX-BDAA4901EA41/Documents/output.txt
I want to clear all images in default cache directory every 1 minute but the cache files do not have extensions to specific their type and I don't know how to delete just images like PNG (not other data).
this is sample code I saw on this site:
let fileManager = FileManager.default
let documentsUrl = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! as NSURL
let documentsPath = documentsUrl.path
do {
if let documentPath = documentsPath
{
let fileNames = try fileManager.contentsOfDirectory(atPath: "\(documentPath)")
print("all files in cache: \(fileNames)")
for fileName in fileNames {
if (fileName.hasSuffix(".png"))
{
let filePathName = "\(documentPath)/\(fileName)"
try fileManager.removeItem(atPath: filePathName)
}
}
let files = try fileManager.contentsOfDirectory(atPath: "\(documentPath)")
print("all files in cache after deleting images: \(files)")
}
} catch {
print("Could not clear temp folder: \(error)")
}
possibility:
you could use ImageIO to test EVERY file before deleting it but that would mean reading it before removing it. It'd replace testing for a suffix BUT
as it'd be really unnecessarily expensive IMHO I wont even provide code.
okay way:
=> Rename your images to have a suffix or prefix so you can identify them by name (n calls)
good way
=> put the images in a seperate folder and just remove the folder to purge them. (1 call)