The file can be very large, but in the case of the crash it is 226.4 MB. Here is how I save the data:
func encodeFile(data: fileTemplate) -> Data { // encodes to json
let rawData:Data = try! JSONEncoder().encode(data)
return rawData
}
let fileData = // setting struct data to save here
let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
var dataToSave:String = ""
var fileURL:URL!
fileURL = URL(fileURLWithPath: fileName, relativeTo: directoryURL).appendingPathExtension("txt")
print("size: \(fileURL.fileSizeString)")
dataToSave = String(data: encodeFile(data: fileData), encoding: .utf8)!
guard let data = dataToSave.data(using: .utf8) else {
print("unable to save the data")
completion?(false)
return
}
// save the data
do {
try data.write(to: fileURL!)
completion?(true)
} catch {
print("Error3: ", error.localizedDescription)
completion?(false)
}