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

[Health 11.1.0] Data duplication issue with entries from multiple devices #1074

Closed
HassanAlyousef opened this issue Nov 4, 2024 · 1 comment · Fixed by #1117
Closed

[Health 11.1.0] Data duplication issue with entries from multiple devices #1074

HassanAlyousef opened this issue Nov 4, 2024 · 1 comment · Fixed by #1117
Assignees

Comments

@HassanAlyousef
Copy link

HassanAlyousef commented Nov 4, 2024

Hello everyone,

I hope all is well with you. I’ve been working with the health plugin for a while now, I've been testing the features with my data, which its source is only my iPhone. One feature sums up the entries for a specific data type for the current day and display them. I used the method below, and it seems to work just fine.

  double _getHealthDataValueToday(HealthDataType type) {
    num totalValue = 0;
    DateTime now = DateTime.now();
    DateTime startOfDay = DateTime(now.year, now.month, now.day, 0, 0, 0);
    DateTime endOfDay = DateTime(now.year, now.month, now.day, 23, 59, 59);

    List<HealthDataPoint> todayData = widget.healthDataList.where((dataPoint) {
      DateTime dataPointStart = dataPoint.dateFrom.toLocal();
      return dataPointStart.isAfter(startOfDay) &&
          dataPointStart.isBefore(endOfDay) &&
          dataPoint.type == type &&
          dataPoint.value is NumericHealthValue;
    }).toList();

    for (var dataPoint in todayData) {
      totalValue += (dataPoint.value as NumericHealthValue).numericValue;
    }

    return totalValue.toDouble();
  }

However, after launching a new TestFlight version, a reported issue indicated that the data was significantly higher than what is shown in Apple’s Health app. After some digging, It looks like some entries are counted twice because they come from more than one source, like an iPhone and an Apple Watch.

I thought the removeDuplicates method would take care of these kind of duplications, but it turns out it only removes entries that are exactly the same in everything, which isn’t really what I was looking for.

Does anyone have an idea on how to tweak my method to avoid counting duplicates? Also, I noticed that the total in my app doesn’t always match the total in Apple Health, even though my math checks out when I add up the entries in the Health app myself. Does anyone know why this might be happening? What method does Apple use for their calculations? Why are some entries left out of their total, even though they all come from one source which is my iPhone? Any help or insight would be great. Thanks!

@HassanAlyousef HassanAlyousef changed the title [Health 11.1.0] Aggregated data are doubled when there re multiple devices inserting health data [Health 11.1.0] Data duplication issue with entries from multiple devices Nov 4, 2024
@iarata iarata self-assigned this Nov 19, 2024
@iarata
Copy link
Contributor

iarata commented Nov 28, 2024

It was mentioned in the #1072 that when there is an Apple Watch paired with iPhone the data in that was returned in the query will be duplicated. This is has been addressed and will be released in the upcoming version.

Duplicate of #1072

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants