title: Execute a function after completing all background process in swift
body: <p>I am using GCD in swift 
  like this : </p>
  
  <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
  
      //all background task
  
      dispatch_async(dispatch_get_main_queue()) {  
          self.second()
      }
  }
  </code></pre>
  
  <p>In this code second function is getting called before completing all background task that's why I am not able to take some data which I am using in second function. I want to second method after completing all background task. Can anyone tell me how to achieve this task?</p>
  
  <p>***************In background I am taking healthkit data like******</p>
  
  <pre><code>let healthKitTypesToRead = 
  Set(
          arrayLiteral: HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
          HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
          HKObjectType.workoutType()
          )
  
  let newCompletion: ((Bool, NSError?) -&gt; Void) = {
          (success, error) -&gt; Void in
  
          if !success {
              print("You didn't allow HealthKit to access these write data types.\nThe error was:\n \(error!.description).")
  
              return
          }
          else
        {
         let stepCount = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
  
                      // Our search predicate which will fetch data from now until a day ago
                      // (Note, 1.day comes from an extension
                      // You'll want to change that to your own NSDate
  
                      //let date = NSDate()
                      //let predicate = HKQuery.predicateForSamplesWithStartDate(date, endDate: NSDate(), options: .None)
  
                      // The actual HealthKit Query which will fetch all of the steps and sub them up for us.
                      let stepCountQuery = HKSampleQuery(sampleType: stepCount!, predicate:.None, limit: 0, sortDescriptors: nil) { query, results, error in
                          var steps: Double = 0
  
                          if results?.count &gt; 0
                          {
                              for result in results as! [HKQuantitySample]
                              {
                                  steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
                              }
                              testClass.HK_stepCount = String(steps)
                          }
  
                          //completion(steps, error)
                      }
  
                      self.healthKitStore.executeQuery(stepCountQuery)
               //EDIT.....
            let tHeartRate = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)
              let tHeartRateQuery = HKSampleQuery(sampleType: tHeartRate!, predicate:.None, limit: 0, sortDescriptors: nil) { query, results, error in
                  if results?.count &gt; 0
                  {
                      var string:String = ""
                      for result in results as! [HKQuantitySample]
                      {
                          let HeartRate = result.quantity
                          string = "\(HeartRate)"
                          print(string)
                      }
                      testClass.HK_HeartRate = string
                        finalCompletion(Success: true)
  
                  }
  
              }
                   self.healthKitStore.executeQuery(tHeartRateQuery)
        }
  
      }
  
  
  healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead, completion: newCompletion)
  </code></pre>
  
  <hr>
  
  <p>I am not able to take value of step count, It get executed after calling second(method) called, plz suggest me what to do?</p>