I want to export all data of my entity "Log". I found this Code Example: https://gist.github.com/kenechilearnscode/2d5d35f550f593332319 But this won't work for me. It don´t add the Data of Core Data, the only output I get is: "This is what the app will export: date, label, typ" My Core Data Entity is "Log" with the attributes: date(type:date), labe1(type:String) and typ(type:Double). How can I export the Core Data to an CSV-File and send it via Mail? Thanks for any help :)
var logs : [Log] = []
func createExportString() -> String {
var date: NSDate? = NSDate()
var labe1: String?
var typ: Double
var export: String = NSLocalizedString("date, label, typ, \n", comment: "")
for (index, log) in logs.enumerated() {
if index < logs.count - 1 {
date = Date() as? NSDate
label = log.value(forKey: "time") as? String
typ = (log.value(forKey: "type") as? Double)!
let dateString = "\(log.date!)"
let labelString = "\(log.labe1!)"
let typeString = "\(log.typ)"
export += dateString + "," + labelString + "," + typeString + "," + "\n"
}
}
print("This is what the app will export: \(export)")
return export
}
func exportDatabase() {
var exportString = createExportString()
saveAndExport(exportString: exportString)
}
func saveAndExport(exportString: String) {
let exportFilePath = NSTemporaryDirectory() + "export.csv"
let exportFileURL = NSURL(fileURLWithPath: exportFilePath)
FileManager.default.createFile(atPath: exportFilePath, contents: NSData() as Data, attributes: nil)
var fileHandleError: NSError? = nil
var fileHandle: FileHandle? = nil
do {
fileHandle = try FileHandle(forWritingTo: exportFileURL as URL)
} catch {
print("Error with fileHandle")
}
if fileHandle != nil {
fileHandle!.seekToEndOfFile()
let csvData = exportString.data(using: String.Encoding.utf8, allowLossyConversion: false)
fileHandle!.write(csvData!)
fileHandle!.closeFile()
let firstActivityItem = NSURL(fileURLWithPath: exportFilePath)
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem], applicationActivities: nil)
activityViewController.excludedActivityTypes = [
UIActivityType.assignToContact,
UIActivityType.saveToCameraRoll,
UIActivityType.postToFlickr,
UIActivityType.postToVimeo,
UIActivityType.postToTencentWeibo
]
self.present(activityViewController, animated: true, completion: nil)
}
}
EDIT:
I try to add these:
let context = DatabaseController.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Log")
let result = try! NSManagedObjectContext.execute(fetchRequest)
logs = [result]
But I get the error: "Use of instance member 'execute' on type 'NSManagedObjectContext'; did you mean to use a value of type 'NSManagedObjectContext' instead?"
EDIT 2:
With these:
do {
let results = try context.execute(fetchRequest)
}
catch {
print(error)
}
I get the error on the line where "logs = [result]: Use of unresolved identifier 'result'