title: Failure to save data to HealthKit (Cocoa error 4097)
body: <p>I'm currently having difficulty saving data to health kit and I'm unsure what the problem is.  I have health kit enabled and have granted read and write permissions on my testing device when the app runs (everything looks OK and I'm able to query the health kit database without error).  I'm using Swift.</p>
  
  <p>The code I'm using to save is:</p>
  
  <pre><code>hksaver.healthStore!.saveObjects(samples, withCompletion: {
          (success: Bool , error: NSError!) -&gt; Void in
          if success { ... } else {
              println("Failed to save. Error: \(error)")
              }
  </code></pre>
  
  <p>samples is an array of HKQuantity Samples.  It is created by appending 'dataSample' as defined below:</p>
  
  <pre><code>let dataPoint = HKQuantity(unit: units, doubleValue: measurement.dataPoint)
  
  let dataSample = HKQuantitySample(type: quantityType,
              quantity: dataPoint, startDate: measurement.startDate,
              endDate: measurement.endDate, metadata: metadata)
  </code></pre>
  
  <p>There are no errors when constructing this, so I believe that all the values are appropriate.</p>
  
  <p>When I run, I'm getting the following error:</p>
  
  <blockquote>
    <p>Failed to save. Error: Error Domain=NSCocoaErrorDomain Code=4097 "The
    operation couldn’t be completed. (Cocoa error 4097.)" (connection to
    service named com.apple.healthd.server) UserInfo=0x17047a0c0
    {NSDebugDescription=connection to service named
    com.apple.healthd.server}</p>
  </blockquote>
  
  <p>I'm trying to write ~100,000 values to HealthKit though I haven't been able to find any limits (the same error occurred when I was trying to write ~50,000 values).  I've tried to write this in chunks, but after around 5 calls to health kit it starts failing (I'm assuming there is a limit to prevent continuous calls although I haven't seen anything in any documentation I've looked at).</p>
  
  <p>I've tried searching under the error code, under the entire error, and under everything I could think of to explain the problem.  The only results I've returned deal with HomeKit and tend to be that it is not activated.  However under the capabilities tab I have HealthKit turned on and everything is check marked so I don't think that is the problem.</p>
  
  <p>Any thoughts would be appreciated!</p>