Created
August 26, 2015 16:13
-
-
Save kenechiokolo/2d5d35f550f593332319 to your computer and use it in GitHub Desktop.
CoreData to CSV file code walkthrough
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: Export functions | |
func createExportString() -> String { | |
var date: NSDate? = NSDate() | |
var worktime: Double? | |
var breaktime: Double? | |
var studyRate: Double? | |
var studyQuota: Double? | |
var hoursLeft: Int? | |
var distractionCount: Int? | |
var breakCount: Int | |
var longestStudy: Double | |
var longestBreak: Double | |
var totalStudyHours: Int | |
var averageDailyStudyHours: Int | |
var sessionArrayBool: [Bool]? = [] | |
var sessionArray: [Double]? = [] | |
var export: String = NSLocalizedString("date, hours studied, breaks, studyRate, study quota, hours left, number of distractions, breaks taken, longest study (hours), longest break (hours), hours studied (last 7 workdays), average study hours a day (last 7 workdays) \n", comment: "") | |
for (index, day) in fetchedStatsArray.enumerate() { | |
if index < fetchedStatsArray.count - 1 { | |
date = day.valueForKey("date") as? NSDate | |
worktime = day.valueForKey("workTotal") as? Double | |
breaktime = day.valueForKey("breakTotal") as? Double | |
studyRate = day.valueForKey("workRateActual") as? Double | |
studyQuota = day.valueForKey("hourQuotaDay") as? Double | |
if studyQuota != nil && worktime != nil { | |
hoursLeft = Int(studyQuota! - (worktime! / 3600) + 0.5) | |
} else { | |
hoursLeft = nil | |
} | |
distractionCount = day.valueForKey("timeInBlock") as? Int | |
sessionArray = day.valueForKey("sessionArray") as? [Double] | |
sessionArrayBool = day.valueForKey("sessionArrayBool") as? [Bool] | |
(longestStudy, longestBreak) = findMaxMinInArray(sessionArray, sessionArrayBool: sessionArrayBool) | |
breakCount = countBreaksTaken(sessionArrayBool) | |
averageDailyStudyHours = calculateAverageHoursLastSevenDays(fetchedStatsArray) | |
totalStudyHours = calculateTotalHoursLastSevenDays(fetchedStatsArray) | |
let dateString = "\(date!)" ?? "-" | |
let worktimeString = "\(round(worktime! / 3600.00))" ?? "-" | |
let breaktimeString = "\(round(breaktime! / 3600.00))" ?? "-" | |
let studyRateString = "\(round(studyRate! * 100.00))" ?? "-" | |
let studyQuotaString = "\(Int(studyQuota! + 0.5))" ?? "-" | |
let hoursLeftString = "\(hoursLeft!)" ?? "-" | |
let distractionCountString = "\(distractionCount!)" ?? "-" | |
let longestStudyString = "\(round(longestStudy / 3600.00))" ?? "-" | |
let longestBreakString = "\(round(longestBreak / 3600.00))" ?? "-" | |
let breakCountString = "\(Int(breakCount))" ?? "-" | |
let avStudyHoursString = "\(Int(averageDailyStudyHours))" ?? "-" | |
let totalStudyHoursString = "\(totalStudyHours)" ?? "-" | |
export += dateString + "," + worktimeString + "," + breaktimeString + "," + studyRateString + "%," + studyQuotaString + "," + hoursLeftString + "," + distractionCountString + "," + longestStudyString + "," + longestBreakString + "," + breakCountString + "," + avStudyHoursString + "," + totalStudyHoursString + "\n" | |
} | |
} | |
print("This is what the app will export: \(export)") | |
return export | |
} | |
func exportDatabase() { | |
var exportString = createExportString() | |
saveAndExport(exportString) | |
} | |
func saveAndExport(exportString: String) { | |
let exportFilePath = NSTemporaryDirectory() + "export.csv" | |
let exportFileURL = NSURL(fileURLWithPath: exportFilePath) | |
NSFileManager.defaultManager().createFileAtPath(exportFilePath, contents: NSData(), attributes: nil) | |
var fileHandleError: NSError? = nil | |
var fileHandle: NSFileHandle? = nil | |
do { | |
fileHandle = try NSFileHandle(forWritingToURL: exportFileURL) | |
} catch { | |
print("Error with fileHandle") | |
} | |
if fileHandle != nil { | |
fileHandle!.seekToEndOfFile() | |
let csvData = exportString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) | |
fileHandle!.writeData(csvData!) | |
fileHandle!.closeFile() | |
let firstActivityItem = NSURL(fileURLWithPath: exportFilePath) | |
let activityViewController : UIActivityViewController = UIActivityViewController( | |
activityItems: [firstActivityItem], applicationActivities: nil) | |
activityViewController.excludedActivityTypes = [ | |
UIActivityTypeAssignToContact, | |
UIActivityTypeSaveToCameraRoll, | |
UIActivityTypePostToFlickr, | |
UIActivityTypePostToVimeo, | |
UIActivityTypePostToTencentWeibo | |
] | |
self.presentViewController(activityViewController, animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment