Failed to save data in HealthKit (Cocoa error 4097)

I am currently having difficulty storing data in a health set, and I'm not sure what the problem is. I have the health set turned on and I have read and write permissions on my testing device when the application starts (everything looks fine, and I can query the health set database without errors). I am using Swift.

The code I use to save:

hksaver.healthStore!.saveObjects(samples, withCompletion: { (success: Bool , error: NSError!) -> Void in if success { ... } else { println("Failed to save. Error: \(error)") } 

sample - an array of HKQuantity samples. It is created by adding "dataSample", as defined below:

 let dataPoint = HKQuantity(unit: units, doubleValue: measurement.dataPoint) let dataSample = HKQuantitySample(type: quantityType, quantity: dataPoint, startDate: measurement.startDate, endDate: measurement.endDate, metadata: metadata) 

There are no errors when building errors, so I believe that all values โ€‹โ€‹are appropriate.

When I run, I get the following error:

Failed to save. Error: Domain error = NSCocoaErrorDomain Code = 4097 "operation could not be completed. (Cocoa error 4097.)" (connection to a service named com.apple.healthd.server) UserInfo = 0x17047a0c0 {NSDebugDescription = connection to a service named com. apple.healthd.server}

I am trying to write ~ 100,000 values โ€‹โ€‹to HealthKit, although I could not find any restrictions (the same error occurred when I tried to write ~ 50,000 values). I tried to write it in chunks, but after 5 calls to the health kit it starts to fail (I assume there is a limit to preventing continuous calls, although I did not see anything in the documentation that I was looking at).

I tried to search by error code, by all error, and under everything that I could come up with to explain the problem. The only results that I returned are related to HomeKit and, as a rule, are not activated. However, on the Features tab, I have HealthKit turned on and everyone is checked, so I don't think this is a problem.

Any thoughts would be appreciated!

+6
source share

Source: https://habr.com/ru/post/983488/


All Articles