title: write data to file with persistent storage, swift
body: <p>I used core data for storing user information, and I tried to write the measurement data (coming from built-in accelerometer) by using "writeToFile".</p>
  
  <p>The core data functioned well with persistent storage of the user data; however, those measurement data file stored by "writeToFile" would just gradually disappear after a long period/newer files were written.</p>
  
  <p>Should I use SQLite or any other database for storing the file of measurement data? Or is it possible to use core data to store files (it seems not achievable though)? I would like to keep both the user data and measurement data into a persistent storage. Thanks for suggestions!!</p>
  
  <pre><code>func writeToFile () {
  
      let firstName = String(trial!.project!)
      let secondName = String(trial!.record!)
  
  
      let exportFilePath = NSTemporaryDirectory() + "\(firstName)_\(secondName).csv"
      let exportFileURL = NSURL(fileURLWithPath: exportFilePath)!
      NSFileManager.defaultManager().createFileAtPath(exportFilePath, contents: NSData(), attributes: nil)
  </code></pre>
  
  <p>Update:
  Problem solved by using "NSFileManager.defaultManager().URLForDirectory". The final code looks like this:</p>
  
  <p>I'm not really sure if you need to state "createDirectoryAtURL" or if the file can be created anyway.</p>
  
  <pre><code>let documentsURL = NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: NSURL(), create: true, error: nil)
  
  NSFileManager.defaultManager().createDirectoryAtURL(documentsURL!, withIntermediateDirectories: true, attributes: nil, error: nil)
  
  let fileURL = documentsURL!.URLByAppendingPathComponent("\(firstName).csv")
  
  NSFileManager.defaultManager().createFileAtPath(fileURL.path!, contents: NSData(), attributes: nil)
  
  let fileHandle = NSFileHandle(forWritingToURL: fileURL, error: nil)
  if let fileHandle = fileHandle {
  ...
  fileHandle.writeData(csvData!)
  ...
  </code></pre>