title: iOS - Swift - sync and async
body: <p>Please help me to solve this problem - after a lot of (not so efficent...) search I can't do this alone.</p>
  
  <p>I have the following methods:</p>
  
  <pre><code>showLoadingAnimation()
  </code></pre>
  
  <p>to show the loading animation while background tasks are running</p>
  
  <pre><code>hideLoadingAnimation()
  </code></pre>
  
  <p>to hide the loading animation as soon as all the background tasks are finished</p>
  
  <pre><code>getUserFacebookData()
  </code></pre>
  
  <p>to get Facebook-user data</p>
  
  <pre><code>uploadUserFacebookDataToServer()
  </code></pre>
  
  <p>to upload the Facebook-user data to the server (and perform tasks with them).</p>
  
  <p>What I want to perform:</p>
  
  <ol>
  <li>Show up the loading animation: <b>showLoadingAnimation()</b></li>
  <li>Get the user data from Facebook: <b>getFacebookData()</b>
  
  <ul>
  <li>Wait until these data are being downloaded</li>
  </ul></li>
  <li>As soon as the Facebook-user data are being download, upload these data to the server: <b>uploadUserFacebookDataToServer()</b>
  
  <ul>
  <li>Wait untile these data are being uploaded</li>
  </ul></li>
  <li>Hide the loading animation: <b>hideLoadingAnimation()</b></li>
  </ol>
  
  <p>Now my problem is, that I don't know how to solve this problem. I know, that I should use <i>sync</i> and/or <i>async</i> tasks, <i>GCD</i>... But I don't know how, and I can't find a proper guide to it.</p>
  
  <p>Could someone explain it to me through these functions?
  Thanks!</p>
  
  <p><b>UPDATE:</b></p>
  
  <p>Thank you, Zhi-Wei Cai, that was the kind of answer what I was hoping for.</p>
  
  <p>Now it seems to work, the calling order is OK, 
  but now the problem is the same as the beginning: </p>
  
  <pre><code>uploadUserFacebookDataToServer()
  </code></pre>
  
  <p>doesn't wait until </p>
  
  <pre><code>getUserFacebookData
  </code></pre>
  
  <p>downloads the user data from Facebook, that's why it won't be able to work with the necessary data given back from</p>
  
  <pre><code>getUserFacebookData
  </code></pre>
  
  <p>Any idea? Is there anything to do with dispatch?</p>
  
  <p><b>UPDATE 2:</b></p>
  
  <p>As you requested, here are the fuctions. I hope, with these information you can help me to solve this problem and to understand this whole process.</p>
  
  <pre><code>func getFacebookUserData(completionHandler: () -&gt; Void)
  {
      println("getFacebookUserData")
  
      let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
      graphRequest.startWithCompletionHandler({ (connection, result, error) -&gt; Void in
  
          if ((error) != nil)
          {
              // Process error
              println("Error: \(error)")
          }
          else
          {
              let userID : NSString = result.valueForKey("id") as NSString!
  
              var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
              defaults.setObject(userID, forKey: "settings_facebookID")
              self.facebookID_fromSettings = userID
          }
      })
  </code></pre>
  
  <p>and</p>
  
  <pre><code>func getObjectIDfromFacebookID(completionHandler: () -&gt; Void)
  {
      println("getObjectIDfromFacebookID")
  
      var query = PFQuery(className:"users")
      query.whereKey("facebookID", equalTo:facebookID_fromSettings)
      println("getObjectIDfromFacebookID: facebookID: " + facebookID_fromSettings)
      query.findObjectsInBackgroundWithBlock {
          (objects: [AnyObject]?, error: NSError?) -&gt; Void in
  
          if error == nil {
              // The find succeeded.
                  println("Successfully retrieved \(objects!.count) scores.")
              // Do something with the found objects
  
              if (objects!.count == 0) {
                  // New user, registering
                  println("New user, registering")
              }
              else
              {
                  //User is already regsitered, reading out objectID
                  println("User is already regsitered, reading out objectID")
              }
  
              if let objects = objects as? [PFObject] {
                  for object in objects {
                      println("objectID: " + object.objectId)
  
                      var objectID: String = object.objectId
                      println(objectID)
  
                      var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
                      defaults.setObject(objectID, forKey: "settings_objectID")
                  }
              }
  
          }
      }
      completionHandler()
  }
  </code></pre>
  
  <p>So the first function gets the facebookID from the FB-server, but this process takes time, it won't give a result immediately. The second function should work with this data, that's why it should "wait" until the first gives back the requested data.</p>
  
  <p>I can solve this problem by building these 2 fuctions together in one, but that's "not elegant", and I also would like to use this (sync/async dispatch) method in other parts of the project,</p>
  
  <p>Thanks for trying to help me!</p>