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

The most recent quantity sample does not update. #115

Open
goztrk opened this issue Nov 12, 2024 · 1 comment
Open

The most recent quantity sample does not update. #115

goztrk opened this issue Nov 12, 2024 · 1 comment

Comments

@goztrk
Copy link

goztrk commented Nov 12, 2024

I have a very simple component to test the incoming data. The problem is, the data is not being updated if the app is active. If I switch to another app and back, the update happens.

I am starting a workout from my Apple Watch and the data is still not being updated.

I also tried to add a refresh button to force the component to re-render but it didn't help.

const HeartRateData = () => {
    const [heartRate, setHeartRate] = React.useState<{
        current: number;
        min: number;
        max: number;
        average: number;
        samples: number[];
    }>({
        current: 0,
        min: 0,
        max: 0,
        average: 0,
        samples: [],
    });
    const heartRateSample = useMostRecentQuantitySample(
        HKQuantityTypeIdentifier.heartRate
    );

    React.useEffect(() => {
        if (heartRateSample) {
            setHeartRate((prev) => {
                const quantity = Math.round(heartRateSample.quantity);
                const samples = [...prev.samples, quantity];
                const min = Math.min(...samples);
                const max = Math.max(...samples);
                const average = Math.round(
                    samples.reduce((acc, curr) => acc + curr, 0) / samples.length
                );
                return {
                    ...prev,
                    current: quantity,
                    samples,
                    min,
                    max,
                    average,
                };
            });
        }
    }, [heartRateSample]);

    const clearData = () => {
        setHeartRate({
            current: 0,
            min: 0,
            max: 0,
            average: 0,
            samples: [],
        });
    };

    const [refresh, setRefresh] = React.useState(0);

    return (
        <Card>
            <Card.Header title="Heart Rate Data" />
            <Card.Content>
                <Text>Current: {heartRate.current}</Text>
                <Text>Min: {heartRate.min}</Text>
                <Text>Max: {heartRate.max}</Text>
                <Text>Average: {heartRate.average}</Text>
                <Text>Count: {heartRate.samples.length}</Text>
                <Text>History: {heartRate.samples.join(', ')}</Text>
                <Text>Refresh: {refresh}</Text>
            </Card.Content>
            <Card.Footer gap={2}>
                <Button flex label="Clear Data" onPress={clearData} />
                <Button
                    flex
                    label="Refresh"
                    onPress={() => setRefresh((prev) => prev + 1)}
                />
            </Card.Footer>
        </Card>
    );
};
@robertherber
Copy link
Member

Are you using the new architecture? In that case #106 might be related.

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

No branches or pull requests

2 participants