Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data chunked before saving to healthkit #145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions HKImport/Importer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,22 +263,52 @@ class Importer: NSObject, XMLParserDelegate {
}

func saveAllSamples() {
saveSamples(samples: self.allSamples, withSuccess: {}, failure: {})
// Chunk the samples and save in batches
let chunkSize = 1000
let chunks = stride(from: 0, to: allSamples.count, by: chunkSize).map {
Array(allSamples[$0..<min($0 + chunkSize, allSamples.count)])
}

saveSamplesInChunks(chunks: chunks, index: 0)
}

private func saveSamplesInChunks(chunks: [[HKSample]], index: Int) {
// Base case: if all chunks are processed, stop
if index >= chunks.count {
os_log("All samples have been saved successfully.")
return
}

// Save the current chunk and proceed to the next one
saveSamples(samples: chunks[index], withSuccess: {
os_log("Successfully saved chunk %d of %d", index + 1, chunks.count)
self.saveSamplesInChunks(chunks: chunks, index: index + 1)
}, failure: {
os_log("Failed to save chunk %d of %d. Proceeding with the next chunk.", index + 1, chunks.count)
self.saveSamplesInChunks(chunks: chunks, index: index + 1)
})
}

func saveSamples(samples: [HKSample], withSuccess successBlock: @escaping () -> Void, failure failureBlock: @escaping () -> Void) {
self.healthStore?.save(samples, withCompletion: { (success, error) in
healthStore?.save(samples) { success, error in
if !success {
if Constants.loggingEnabled {
os_log("An error occured saving the sample. The error was: %@.", error.debugDescription)
if let error = error {
os_log("An error occurred saving the samples. Error: %@", error.localizedDescription)
} else {
os_log("An unknown error occurred saving the samples.")
}
}
failureBlock()
return
}

// Update UI and call the success block
DispatchQueue.main.async {
self.writeCounterLabel?.text = "\(Int((self.writeCounterLabel?.text)!)! + samples.count)"
self.writeCounterLabel?.text = "\(Int(self.writeCounterLabel?.text ?? "0")! + samples.count)"
}
successBlock()
})
}
}

}