StanfordSpezi / SpeziTemplateApplication

Template application demonstrating the usage of the Stanford Spezi framework.
https://stanfordspezi.github.io/SpeziTemplateApplication
MIT License
97 stars 19 forks source link

Bug report: HealthKit Sample Upload Before Firebase Login #72

Open MarcSchlichting opened 6 months ago

MarcSchlichting commented 6 months ago

Description

When closing the app entirely (not running in the background) and opening it again, some HK samples can get lost as they are never transferred to Firebase. From reading the log messages (error message: Could not store HealthKit Sample: Error Domain=Standard.StandardError Code = 0). I believe that this is due to the app trying to push data to Firebase before the app is authenticated with Firebase.

Reproduction

Closing the app entirely (in the simulator and a real device), not just pressing the home button (or the equivalent for the newer iPhones), creating a new HealthKit sample (e.g., manually adding it in the Health App) and then re-opening the template application causes the error. The error message can be seen when the console entries are logged or through observing the Firebase backend.

Expected behavior

The authentication should happen first and only after the app is correctly authenticated, a sample transfer should happen and no samples should be lost.

Additional context

No response

Code of Conduct

PSchmiedmayer commented 6 months ago

@Supereg We have resolved this in the TBI app by adding the Firebase configuration module as a dependency of the Standard and manually using the Firebase functions to obtain a cached user.

It would be good to explore how we might automate that setup that Spezi Firebase Auth code and Account injection somehow is guaranteed to run before a HealthKit data type could arrive?

Supereg commented 6 months ago

So what you are doing in TBI is directly accessing Auth.auth().currentUser which is present from the start?

What we currently do in SpeziFirebase is to force refresh the auth token upon app startup. This code was kept from the original version of SpeziAccount/SpeziFirebase. We could choose to remove that altogether, which should improve configuration time of the SpeziFirebase module. I assume, Firebase would automatically refresh the token before you do the next request.

PSchmiedmayer commented 6 months ago

Yes, that is the current approach we follow in TBI.

I think it is good to inject the account as early as possible. We can still do the token refresh once we already set the user as signed in and provided all the information right on the startup?

Supereg commented 6 months ago

I'm not sure where the original rationale came from to refresh the token upon app start. I think it might make sense to keep that to e.g. have a smooth experience when there are external changes to your account.

But I agree, we should aim for getting the Account details ready faster (at least partially, e.g., it might take a while to load the user details from an external storage provider. Otherwise, we could choose to cache that on disk as well.)

PSchmiedmayer commented 2 months ago

This issue is related to https://github.com/StanfordSpezi/SpeziFirebase/issues/29; closing this issue should resolve this behavior in the Template App.