ios - Might somebody please assist me perceive how to ensure this code runs within the appropriate order? - Slsolutech Best IT Related Website google.com, pub-5682244022170090, DIRECT, f08c47fec0942fa0

ios – Might somebody please assist me perceive how to ensure this code runs within the appropriate order?

Spread the love


I’ve this code to get knowledge from the well being retailer. I’m looping by way of data for every of the totally different pattern sorts that I need to get from the well being retailer within the fetchSampleData perform. I’m anticipating the perform to loop by way of and get all the totally different sorts of samples after which use the completion to ship that knowledge again to the getData perform. I then need to use that knowledge in my code.

func getData() {
    // Knowledge sorts and items for well being equipment queries
    let sampleInfo = [
        ("heartRate", HKObjectType.quantityType(
                forIdentifier: .heartRate), "count/min"),
        ("heartRateVariabilitySDNN", HKObjectType.quantityType(
                forIdentifier: .heartRateVariabilitySDNN), "s"),
        ("stepCount", HKObjectType.quantityType(
                forIdentifier: .stepCount), "count"),
    ]
        
    self.fetchSampleData(
        sampleInfo: sampleInfo
    ) { samplesDict in

          let sampleCount = samplesDict.rely

          print("Pattern rely: (sampleCount)")
          print(samplesDict)

          if (sampleCount > 0) {
              // code to do stuff with samplesDict ...
          }
        
    }
}

public func fetchSampleData(
                sampleInfo: [(String, HKQuantityType?, String)],
                completion: @escaping ( _ samples: 
                    Dictionary<String, Dictionary<Date, Double>>) -> Void
    ) {
    
    let numberOfDataTypes = sampleInfo.rely
    var resultsDict: [String: [Date: Double]] = [:]
    
    for i in 0..<numberOfDataTypes {
        let dataTypeName = sampleInfo[i].0
        let sampleType = sampleInfo[i].1!
        let unitString = sampleInfo[i].2
        var dataTypeDictionary = [Date:Double]()

        // Predicate for specifying begin and finish dates for the question
        let predicate = HKQuery
            .predicateForSamples(
                withStart: Date.distantPast,
                finish: Date.now,
                choices: .strictEndDate)
        
        // Set sorting by date.
        let sortDescriptor = NSSortDescriptor(
            key: HKSampleSortIdentifierStartDate,
            ascending: false)
        
        // Create the question
        let question = HKSampleQuery(
            sampleType: sampleType,
            predicate: predicate,
            restrict: Int(HKObjectQueryNoLimit),
            sortDescriptors: [sortDescriptor]) { (_, outcomes, error) in
                
                guard error == nil else {
                    print("Error: (error!.localizedDescription)")
                    return
                }

                print("~~- (outcomes?.rely) (dataTypeName) samples returned")
                for pattern in outcomes ?? [] {
                    let knowledge = pattern as! HKQuantitySample
                    let unit = HKUnit(from: unitString)
                    let sampleVal = knowledge.amount.doubleValue(for: unit)
                    let dateStart = knowledge.startDate
                    
                    dataTypeDictionary[dateStart] = sampleVal
                }

                resultsDict[dataTypeName] = dataTypeDictionary
            }
        
        healthStore.execute(question)
    }

    completion(resultsDict)
}

However after I run this code I get this for an output:

Pattern rely: 0

[:]

~~- Non-obligatory(103) heartRateVariabilitySDNN samples returned

~~- Non-obligatory(846) stepCount samples returned

~~- Non-obligatory(3127) activeEnergyBurned samples returned

~~- Non-obligatory(4470) heartRate samples returned

~~- Non-obligatory(2913) basalEnergyBurned samples returned

So the code that i used to be assuming would run after the fetchSampleData perform was accomplished appears to be working earlier than the info is even returned. Is there a method that I may inform this system to solely run the code involving the returned samplesDict knowledge if the fetchSampleData has completed getting that knowledge?

Leave a Reply

Your email address will not be published. Required fields are marked *