2017-03-03 84 views
2

我正在使用手机记录一些传感器数据,并通过SQLKit通过SharkORM(DBAccess)将其存储在设备上。Swift:Streaming /撰写CSV文件

我现在想将这些数据写入CSV文件,但是现在我已经达到了160万条记录。

目前,我正在循环1000条记录,将它们添加到一个字符串,并在最后写出它们。但是,一定有更好的方法来做到这一点?

func writeRawFile() 
    { 
     let fileName = "raw" 
     let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) 

     let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("csv") 

     var data = "time,lat,lon,speed,x_acc,y_acc,z_acc,gyro_x,gyro_y,gyro_z,compass,orientation\r\n" 

     let count = RawReading.query().count() 
     var counter = 0; 
     let df = DateFormatter() 
     df.dateFormat = "y-MM-dd H:m:ss.SSSS" 

     for i in stride(from:0, to: count, by: 1000) 
     { 
      autoreleasepool { 

       for result in RawReading.query().offset(Int32(i)).limit(1000).fetch() 
       { 
        if let raw : RawReading = result as? RawReading 
        { 
         if (Double(raw.speed!) > 3.0) //1 Meter per Second = 2.236936 Miles per Hour 
         { 
          //print(df.string(from: raw.date!)) 

          data += "\(df.string(from: raw.date!)),\(raw.lat!),\(raw.lon!),\(raw.speed!),\(raw.x!),\(raw.y!),\(raw.z!),\(raw.xx!),\(raw.yy!),\(raw.zz!),\(raw.compass!),\(raw.orientation!)" + "\r\n" 

          counter += 1 
         } 

        } 
       } 

       print ("Written \(i) of \(count)") 

      } 
     } 

     print("Count \(count) \(counter)") 

     //write the file, return true if it works, false otherwise. 
     do{ 
      try data.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8) 
     } catch{ 
      print("error") 
     } 
    } 

回答

5

打开用于写入的FileHandle,然后生成并分别写每一行,这样你就不必保持整个文件的内容 内存:

do { 
    let file = try FileHandle(forWritingTo: fileURL) 
    defer { file.closeFile() } 

    for <... your loop ...> { 
     let line = ... // build one CSV line 
     file.write(line.data(using: .utf8)!) 
    } 

} catch let error { 
    // ... 
} 

你也可以写首先是一个临时文件,然后将其重命名为 实际文件,以避免 出错时出现损坏的文件。