Post

Replies

Boosts

Views

Activity

How can I get the highest sample rate historical heart rate data from health store?
I am trying to get heart rate data from health store but when I make a query there are samples that come through with timestamps that shows that there are minutes that go by between the sample measurements. I remember watching an apple video where they were talking about the Quantity series and how these series have one kind of container for multiple data points of bpm data. I also see in the samples that there is a unit of " ___ counts per second" for each of the results so it would make sense if there were multiple datapoints within each of the results from the query but I don't know how to see what's inside of the results or if there is even anything more to them. This is the code I am using to retrieve the data: public func fetchLatestHeartRateSample(completion: @escaping (_ samples: [HKQuantitySample]?) -> Void) { /// Create sample type for the heart rate guard let sampleType = HKObjectType .quantityType(forIdentifier: .heartRate) else { completion(nil) return } /// Predicate for specifying start and end dates for the query let predicate = HKQuery .predicateForSamples( withStart: Date.distantPast, end: Date(), options: .strictEndDate) /// Set sorting by date. let sortDescriptor = NSSortDescriptor( key: HKSampleSortIdentifierStartDate, ascending: false) /// Create the query let query = HKSampleQuery( sampleType: sampleType, predicate: predicate, limit: Int(HKObjectQueryNoLimit), sortDescriptors: [sortDescriptor]) { ( _, results, error) in guard error == nil else { print("Error: \(error!.localizedDescription)") return } print(results ?? "Error printing results.") completion(results as? [HKQuantitySample]) } healthStore.execute(query) } Ultimately I want the most frequent heart rate data that is available. Ideally a bpm measurement every 5 - 10 seconds or less. Is this possible if I have not explicitly saved this heart rate data to the health store for a user before trying to access it or does Apple store all of this information in the health store regardless of the circumstances? Any help is greatly appreciated!
3
0
1k
Sep ’23
Is this a good approach for creating this Apple Watch sleep game feature?
I am creating a watch app that keeps track of the amount of time a user has been asleep for and then updates a game using an API in real time if they surpass another user's sleep time. They select who they want to surpass so the app knows the target time they want to sleep for. I know this probably sounds silly, it's not the final iteration of what I want to build but I know I will need this functionality. So I need this watch app to be able to update the game if they surpass the opponent while the user is sleeping. I don't think this is possible without using extended runtime sessions so that it can run in the background and check to see how long the user has been asleep for after the watch locks. As far as I know the extended runtime sessions only last for 30 minutes though. My current plan to deal with this is to reschedule the session using session.start(at: ) I am currently thinking about doing it like this: User selects opponent and goes to bed App gets the amount of time the user needs to sleep for to surpass opponent, "sleepTime" App schedules an extended runtime session to activate sleepTime seconds in the future sleepTime seconds in the future the session activates and the app checks to see how long the user has actually slept since they went to bed. If the user has slept less than the sleepTime needed to surpass the opponent the app will reschedule the extended runtime session for sleepTime - amountSleptSoFar == "sleepRemaining" seconds, in the future. sleepRemaining seconds in the future the session will activate and check to see how long the user has actually slept for again. If the user has slept less than the time needed to surpass the opponent then the session is rescheduled like before If the user has slept more than the time needed to surpass the opponent then the app uses an API to update the game. It is important that the game is updated as soon as the user surpasses an opponent and I don't want to set up a database to store sleep information and then update the game using the database data as soon as the database is updated. Does this seem like an okay approach? And more importantly is it possible to do? From what I've read and implemented so far it seems possible but please let me know if I'm missing something. The one thing I'm unsure about is if I can use session.start(at: ) when the app is not currently open. If I can't reschedule using that then how else could I do what I need to do?
0
0
650
Aug ’23