How to handle multiple queries of different HKQuantityTypeIdentifier - swift

I'm trying to get multiple types of health data all with the same timeframe. My problem relies on the way that I should handle all the data that I get from those queries. Currently my model is as follows:
struct DailyData {
var steps: Int?
var distance: Int?
var calories: Int?
var exercise: Int?
}
class UserData {
let healthManager = HealthKitManager.shared
var dailyData = [DailyData]?
.
.
.
}
if I'm not mistaken, I can only query only one HKQuantityIdentifier at a time, so that means I need to call my getData() function from my HealthKitManager.swift once for every HKQuantityType that I have in my model:
func getData(type: HKQuantityTypeIdentifier, unit: HKUnit, days: Int, completed: #escaping (Result<[Int], Error>) -> Void) {
let calendar = NSCalendar.current
let interval = NSDateComponents()
interval.day = 1
let quantityType = HKQuantityType.quantityType(forIdentifier: type)!
var anchorComponents = calendar.dateComponents([.day, .month, .year], from: NSDate() as Date)
anchorComponents.hour = 0
let anchorDate = calendar.date(from: anchorComponents)
// Define 1-day intervals starting from 0:00
let query = HKStatisticsCollectionQuery(quantityType: quantityType,
quantitySamplePredicate: nil,
options: .cumulativeSum,
anchorDate: anchorDate!,
intervalComponents: interval as DateComponents)
query.initialResultsHandler = {query, results, error in
if let error = error {
completed(.failure(error))
return
}
let endDate = NSDate()
let startDate = calendar.date(byAdding: .day, value: -(days - 1), to: endDate as Date, wrappingComponents: false)
var completeDataArray: [Int] = []
if let myResults = results{
myResults.enumerateStatistics(from: startDate!, to: endDate as Date) { statistics, stop in
if let quantity = statistics.sumQuantity(){
let dayData = quantity.doubleValue(for: unit)
completeDataArray.append(Int(dayData))
}
}
}
completed(.success(completeDataArray))
}
healthStore.execute(query)
}
My problem is that I can't find a way to correctly set the received data into my model. Could someone point me in the right direction? I believe my model could be wrong, because as for what I have gather online, it's impossible to query multiple HKQuantityTypes in one query. Meaning that I would definitely have to set my model one [Int] of a HKtype at a time.
But with what I currently have, that would mean that when the first query returns I have to create multiple DailyData objects with almost all of the variables nil except the one I'm setting. Then, when the other queries return, I should do some array checking of dailyData matching the .count values with the one I got from the query, and that just feels wrong in my opinion. Not very elegant.
I tried another approach. Instead of having an array of a custom type, having a custom type that inside has an array of ints for every HKType that I need. But this has another problem: How could I "Keep in sync" the data from every HKType? having different arrays for every type would be, in my opinion, difficult to handle for example in a tableView. How could I set number of rows in section? Which type should I prefer? Also I would be accessing data over array indexes which may lead to bugs difficult to solve.
struct WeekData {
var steps: [Int]?
var distance: [Int]?
var calories: [Int]?
var exercise: [Int]?
}
class UserData {
let healthManager = HealthKitManager.shared
var weekData = WeekData()
.
.
.
}

Related

Unexpected behaviour - unable to access new elements in an array driven by an NSFetchedResultController (SwiftUI)

I have a SwiftUI app that uses the MVVM design pattern in places where the underlying logic driving the View is either verbose or unit testing is advisable. In certain places I have taken to using a NSFetchedResultsController in conjunction with #Published properties and, early in development, this behaved as I would expect.
However, I have now encountered a situation where an addition to the CoreData store triggers controllerDidChangeContent and the array populated by controller.fetchedObjects has an appropriate number of elements but, for reasons I cannot fathom, I am unable to access the newest elements.
There is a certain amount of data processing which, as I'm working with an array by this point, I didn't think would cause a problem. I'm more suspicious that relationships may be responsible in some way and/or faulting is responsible (although adjusting faulting behaviour on the underlying fetch request failed to resolve the issue).
Interestingly, some similar code elsewhere in the app that uses #FetchRequest (because the View is simpler and so a ViewModel wasn't considered necessary) doesn't seem to suffer from the same problem.
Normally scattering debugging around has put me back on track but not today! I've included the console output - as you can see, as new entries (timestamped) are added, the total observation count increases but the most property which should reflect the most recent observation does not change. Any pointers would be gratefully received as always.
I can't really prune the code on this without losing context - apologies in advance for the verbosity ;-)
ViewModel:
extension ParameterGridView {
final class ViewModel: NSObject, ObservableObject, NSFetchedResultsControllerDelegate {
#Published var parameters: [Parameter] = []
#Published var lastObservation: [Parameter : Double] = [:]
#Published var recentObservation: [Parameter : Double] = [:]
let patient: Patient
private let dataController: DataController
private let viewContext: NSManagedObjectContext
private let frc: NSFetchedResultsController<Observation>
var observations: [Observation] = []
init(patient: Patient, dataController: DataController) {
self.patient = patient
self.dataController = dataController
self.viewContext = dataController.container.viewContext
let parameterFetch = Parameter.fetchAll
self.parameters = try! dataController.container.viewContext.fetch(parameterFetch)
let observationFetch = Observation.fetchAllDateSorted(for: patient)
self.frc = NSFetchedResultsController(
fetchRequest: observationFetch,
managedObjectContext: dataController.container.viewContext,
sectionNameKeyPath: nil,
cacheName: nil)
try! self.frc.performFetch()
observations = self.frc.fetchedObjects ?? []
super.init()
frc.delegate = self
updateHistoricalObservations()
}
// MARK: - METHODS
/// UI controls for entering new Observations default to the last value entered
/// This function calculates the median value for the Parameter's reference range to be used in the event no historical observations are available
/// - Parameter parameter: Parameter used to derive start value
/// - Returns: median value for the Parameter's reference range
func medianReferenceRangeFor(_ parameter: Parameter) -> Double {
let rangeMagnitude = parameter.referenceRange.upperBound - parameter.referenceRange.lowerBound
return parameter.referenceRange.lowerBound + (rangeMagnitude / 2)
}
/// Adds a new Observation to the Core Data store
/// - Parameters:
/// - parameter: Parameter for the observation
/// - value: Observation value
func addObservationFor(_ parameter: Parameter, with value: Double) {
_ = Observation.create(in: viewContext,
patient: patient,
parameter: parameter,
numericValue: value)
try! viewContext.save()
}
/// Obtains clinically relevant historical observations from the dataset for each Parameter
/// lastObservation = an observation within the last 15 minutes
/// recentObservation= an observation obtained within the last 4 hours
/// There may be better names for these!
private func updateHistoricalObservations() {
let lastObservationTimeLimit = Date.now.offset(.minute, value: -15)!.offset(.second, value: -1)!
let recentObservationTimeLimit = Date.now.offset(.hour, value: -4)!.offset(.second, value: -1)!
Logger.coreData.debug("New Observations.count = \(self.observations.count)")
let sortedObs = observations.sorted(by: { $0.timestamp < $1.timestamp })
let newestObs = sortedObs.first!
let oldestObs = sortedObs.last!
Logger.coreData.debug("Newest obs: \(newestObs.timestamp) || \(newestObs.numericValue)")
Logger.coreData.debug("Oldest obs: \(oldestObs.timestamp) || \(oldestObs.numericValue)")
for parameter in parameters {
var twoMostRecentObservatonsForParameter = observations
.filter { $0.cd_Parameter == parameter }
.prefix(2)
if let last = twoMostRecentObservatonsForParameter
.first(where: { $0.timestamp > lastObservationTimeLimit }) {
lastObservation[parameter] = last.numericValue
twoMostRecentObservatonsForParameter.removeAll(where: { $0.objectID == last.objectID })
} else {
lastObservation[parameter] = nil
}
recentObservation[parameter] = twoMostRecentObservatonsForParameter
.first(where: { $0.timestamp > recentObservationTimeLimit })?.numericValue
}
}
// MARK: - NSFetchedResultsControllerDelegate conformance
internal func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
let newObservations = controller.fetchedObjects as? [Observation] ?? []
observations = newObservations
updateHistoricalObservations()
}
}
}
NSManagedObject subclass:
extension Observation {
// Computed properties excluded to aid clarity
class func create(in context: NSManagedObjectContext,
patient: Patient,
parameter: Parameter,
numericValue: Double? = nil,
stringValue: String? = nil) -> Observation {
precondition(!((numericValue != nil) && (stringValue != nil)), "No values sent to initialiser")
let observation = Observation(context: context)
observation.cd_Patient = patient
observation.timestamp = Date.now
observation.parameter = parameter
if let value = numericValue {
observation.numericValue = value
} else {
observation.stringValue = stringValue!
}
try! context.save()
return observation
}
static var fetchAll: NSFetchRequest<Observation> {
let request = Observation.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(keyPath: \Observation.cd_timestamp, ascending: true)]
return request
}
static func fetchAllDateSorted(for patient: Patient) -> NSFetchRequest<Observation> {
let request = fetchAll
request.sortDescriptors = [NSSortDescriptor(keyPath: \Observation.cd_timestamp, ascending: true)]
request.predicate = NSPredicate(format: "%K == %#", #keyPath(Observation.cd_Patient), patient)
return request
}
static func fetchDateSorted(for patient: Patient, and parameter: Parameter) -> NSFetchRequest<Observation> {
let patientPredicate = NSPredicate(format: "%K == %#", #keyPath(Observation.cd_Patient), patient)
let parameterPredicate = NSPredicate(format: "%K == %#", #keyPath(Observation.cd_Parameter), parameter)
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [patientPredicate, parameterPredicate])
let request = fetchAll
request.predicate = compoundPredicate
return request
}
}
Console output: (note observation count increments but the most recent observation does not change)
There is something wrong with your timestamps and/or sorting, the oldest observation is 4 days newer than the newest one (and it is in the future!)
Joakim was on the money - the timestamps are indeed incorrect; the problem was not in the logic but an error in the code (maths error relating to the TimeInterval between datapoints) that generated data for testing purposes. Garbage in, garbage out...
A lesson to me to be more careful - precondition now added to the function that generated the time series data (and a unit test!).
static func placeholderTimeSeries(for parameter: Parameter, startDate: Date, numberOfValues: Int) -> [(Date, Double)] {
let observationTimeInterval: TimeInterval = (60*5) // 5 minute intervals, not 5 hours! Test next time!!
let observationPeriodDuration: TimeInterval = observationTimeInterval * Double(numberOfValues)
let observationEndDate = startDate.advanced(by: observationPeriodDuration)
precondition(observationEndDate < Date.now, "Observation period end date is in the future")
return placeholderTimeSeries(valueRange: parameter.referenceRange,
valueDelta: parameter.controlStep...(3 * parameter.controlStep),
numberOfValues: numberOfValues,
startDate: startDate,
dataTimeInterval: observationTimeInterval)
}

I have a database, if I want to retrieve a field of a specific document I can, but I would like to retrieve a field from the last document updated

In this way it works, I go to retrieve the field of that document (in this case the extranote), but what if I would only be interested in that field of the last document inserted chronologically?
I looked for similar examples but I wouldn't want just a few options, which I obviously don't know, to add when I create the db.
This model was created to read directly into the database without going through the App:
struct WorkoutModel: Identifiable,Codable {
//var id = UUID().uuidString
#DocumentID var id : String?
var extranote : String
var error: String = ""
}
struct SimpleEntry: TimelineEntry {
let date: Date
var deviceCount: Int
var deviceMake: String
var deviceModel: String
var deviceType: String
let configuration: ConfigurationIntent
var workoutData: WorkoutModel?
}
func fetchFromDB(completion: #escaping (WorkoutModel)->()){
let db = Firestore.firestore().collection("devices").document("lEp5impGTGeBmAEisQT")
db.getDocument { snap, err in
guard let doc = snap?.data() else {
completion(WorkoutModel(extranote: "", error: err?.localizedDescription ?? ""))
return
}
let extranote = doc["extranote"] as? String ?? ""
completion(WorkoutModel(extranote: extranote))
}
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context,
completion: #escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting
from the current date.
let date = Date()
let nextUpdate = Calendar.current.date(byAdding: .minute, value: 5, to:date)!
fetchFromDB { work in
let entry = SimpleEntry(date: nextUpdate, deviceCount:
deviceCount,deviceMake: deviceMake,deviceModel: deviceModel,deviceType:
deviceType, configuration: configuration, workoutData: work)
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
If you don't know the ID of the document to update, then you will need to query the collection for the document. If you don't have a field in that document that indicates the timestamp of its last update, then you won't be able to make that query at all. So, you will have to add a field to the document, and make sure it's populated with the current timestamp every time you write it. Then you can use that field to make the query if you want to update it again later.

How do I write a swift function that will count the number of days without an entry?

I am working on a project with NSManagedObjects where each object is a user entry that has a value and a date. The app will be running a 7 day average and a 14 day average of the values the user enters. I have already set it up to do the appropriate fetch requests, sum the values, and divide by 7 and 14, respectively. However, I am realizing that when the user first begins using the app these running average values will be very misleading, so I would like to set up a function that will evaluate the number of days out of the last 7 and 14 that do not have any entries so I can subtract that value from the denominator in these calculations. I am a relative beginner and am having a hard time getting my head around how to write this function though, so any help would be appreciated. Thanks in advance.
Edit in response to Drekka:
The code I'm working from is below. I'm sorry for the broad question but I can't quite figure out where to start with structuring a looping function for what I'm trying to do and I haven't been able to come up with any examples or analogues in the searching I've done. Basically I'm pulling all of the values entered in the last seven days but I'm trying to figure out a way to evaluate for days where no values were entered within the span of this fetch request.
func sevenDayFetch() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Entry")
var calendar = Calendar.current
calendar.timeZone = NSTimeZone.local
let sevenDaysAgo = calendar.date(byAdding: .day, value: -7, to: Date())
let dateFrom = calendar.startOfDay(for: sevenDaysAgo!)
let dateTo = Date()
let fromPredicate = NSPredicate(format: "entryDate > %#", dateFrom as NSDate)
let toPredicate = NSPredicate(format: "entryDate <= %#", dateTo as NSDate)
let sevenDayPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [fromPredicate, toPredicate])
fetchRequest.predicate = sevenDayPredicate
do {
entryArray = try managedContext.fetch(fetchRequest)
var sevenDayArray: [Int] = []
for i in entryArray as [NSManagedObject] {
sevenDayArray.append(i.value(forKey: "Value") as! Int)
}
let sevenDaySum = sevenDayArray.reduce(0, +)
let sevenDayAverage = sevenDaySum/7
sevenDayAverageLabel.text = String(sevenDayAverage)
I find it helpful to save the install date of the app in UserDefaults.
func installDate() -> NSDate {
var installDate: Date
if let date = UserDefaults.standard.object(forKey: UserDefaultsKeys.dateInstalled) as? Date {
installDate = date
} else {
installDate = Date()
UserDefaults.standard.set(installDate, forKey: UserDefaultsKeys.dateInstalled)
}
return installDate
}
Core data is not well suited for storing a single global value. And you cannot infer the install date from core-data; not having a value for a date does not mean that the was not install then.
Once you know the install date you can adjust calculations and your UI, if it is less than 14 or 7 days.

Proper way to save a DateInterval to Realm

Realm doesn't support DateInterval to be store into the database. For now our team do the following:
private let _intervalBegins = List<Date>()
private let _intervalEnds = List<Date>()
var dateIntervals: [DateInterval] {
get {
var intervals = [DateInterval]()
for (i, startDate) in _intervalBegins.enumerated() {
let endDate = _intervalEnds[i]
intervals.append(DateInterval(start: startDate, end: endDate))
}
return intervals
}
set {
_intervalBegins.removeAll()
_intervalBegins.append(objectsIn: newValue.compactMap{ $0.start })
_intervalEnds.removeAll()
_intervalEnds.append(objectsIn: newValue.compactMap{ $0.end })
}
}
Is there a more "proper" way to do this? Maybe to store both the start and end dates into one property/database column? And get those value directly without "parsing" them with another variable as we do now.
Thanks!
As you notice, Realm doesn't support DateInterval, but Realm is able to save your custom objects. In this case you can create your own RealmDateInterval (or so) and create initializer, that allows you to create object from DateInterval:
dynamic var start: Date = Date()
dynamic var end: Date = Date()
convenience init(dateInterval: DateInterval) {
self.init()
self.start = dateInterval.start
self.end = dateInterval.end
}
Next thing, when you retrieve RealmDateInterval from Realm you really want DateInterval instead. Here you can create a bridge function, that can convert RealmDateInterval to DateInterval or create a protocol with convert func and
adopt it to RealmDateInterval (i.e. clearly show everybody RealmDateInterval has specific functionality).
protocol DateIntervalConvertible {
func toDateInterval() -> DateInterval
}
extension RealmDateInterval: DateIntervalConvertible {
func toDateInterval() -> DateInterval {
return DateInterval(start: start, end: end)
}
}

pulling not consistent data in firebase

I am working on a firebase project and decided to make some changes in the data structure. Now, I decided it would be better to add a placeholder image to the node below (Before, I used a dummy image to serve as a placeholder). Is there a way I can still get the dates below? and add the placeholder URL to another structure?
So what I would want is an array dates = ["20180203","20180204","20180205"] and then another array containing placeholders = ["https.googleapis.whateverIsInTheDay80180203", "https.dummydefaultimage","https.dummydefaultimage"]
public func getAvailableDates(spotTitle:String, handler: #escaping (_ dateList:[String])->())
{
var datesList:[String] = []
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd"
let allowedDays = 30
ref.child("ImageLocationDates").child(spotTitle).observe(DataEventType.value, with: { (snapshot) in
if snapshot.childrenCount > 0 {
for mydata in snapshot.children.allObjects as! [DataSnapshot]
{
let date = mydata.key
if date.count == 8 {
let testDate = formatter.date(from: date)
let cal = Calendar.current
let components = cal.dateComponents([.day], from: testDate!, to: cal.startOfDay(for: Date()))
let dateDistance = components.day! // integer of distance between today and date provided
if dateDistance < allowedDays {
if !datesList.contains(convertDate(stringDate: date)){
datesList.insert(convertDate(stringDate: date), at: 0)
}
else {
print("getting duplicate data")
}
}
}
}
handler(datesList)
}
})
}
In Firebase real time database you can have a few ways to structure you data. Keep in mind you want your data not to get too deep. Usually my limit is 3 folds not more. To keep things shallow then, you can normalize and distribute your data which includes a bit of duplication sometimes but it can speed up queries and will decrease the amount of data users download each time.
Look at three structures below :
(A)
D_street:
2012491203 :
placeholder: "https://...."
2012491203 :
placeholder: "https://...."
...
(B)
D_street_dates:
2012491203 : true
2012491203 :
...
D_street_placeholder:
2012491203 : "https://...."
2012491203 : "https://...."
...
(C)
D_street_dates:
id1 :
date: 2012491203
placeholder: "https://...."
id2 :
date: 2012491203
placeholder: "https://...."
...
Each one of structures above can be great depending on your use case. in A you can query by the date (key) and once you get the results you have actually downloaded the placeholders too because they are the values.
In B you only download date when you query D_street_dates and then you have to do another query to get the placeholder per each date from D_street_placeholder.
In C you structure data per an ID or userID or something that both date and placeholders are properties of and whenever you query ids you get both dates and placeholder for that.