I have a heart rate device connected to my app and concerned about losing heart rates in different scenarios.
When the app is in the foreground, I'm streaming heart rates and storing them in a 3rd party database. That's the easy part.
Things get complicated when the app goes into the background while the user is working out etc, and heart rates are still streaming. The file system becomes encrypted so I can't write to the database, so my idea is to append heart rates to a text file, then pull them into the database when the app comes back to the foreground.
It still gets more complicated because the heart rates are once a second, so there can be tens of thousands before the app comes back. I'm thinking instead of appending them to a text file, just to create file names that indicate the heart rate number and derive the data from the file names and creation date. That way, I never have to open a file. I just have to read it's meta data and delete the file when read into the database.
For such a seemingly simple task, it gets even more complicated. I have to deal with Core Bluetooth and ensure it keeps the connection open, but I can't imagine iOS will allow the app to listen to the heart rate device indefinitely. At some point the app will terminate, but what happens to the heart rate stream?
Is there a proven way to handle streaming heart rates in the background indefinitely? Any advise or experience would be greatly appreciated!