carekit-apple / CareKit

CareKit is an open source software framework for creating apps that help people better understand and manage their health.
https://www.researchandcare.org
Other
2.41k stars 444 forks source link

Watch remote synchronization fails with duplicate Outcome. #700

Open Dlemex opened 1 year ago

Dlemex commented 1 year ago

There is an error in the CareKitStore: OCKCDOutcome validateForInsert() function. During synchronization, it is possible for two outcomes for the same task/taskOccurrenceIndex values where one is the previous outcome. The validation correctly handles the case where the next set is empty. However, when the validation occurs for the previous outcome, the validation will see the other outcome as a duplicate outcome.

This issue has been dogging me since version 2.x, and I finally tracked it down. The solution is to add after the guard let:

   if next.count > 0 {
      return
   }

However, before fixing this, the context save in synchronization failing (due to the duplicate outcome) would break future synchronization attempts. The next synchronization attempt would crash because the task is missing when creating an outcome (when the task is forcibly unwrapped). As the above fixes my issue, I did not investigate further, but there seems to be an issue if the try self.context.save() fails in pullThenPush(completion:) step #5.