title: Can't load data into ViewController from a JSON response swift
body: <p>I have an issue understanding the process in consuming REST api services with SWIFT, seems like i'm missing something simple, but yet important here. </p>
  
  <p>this is the singleton DataManager class, I'm using to consume API with loadNews() method, as you can see it's simple, request method, getter and initializer that will load the data.</p>
  
  <p>for loadNews() I use Alamofire to handle request, and SwiftyJSON to parse the response.</p>
  
  <pre><code>class DataManager{
      static let shared = DataManager()
      private var data:JSON = JSON()
      private init(){
          print("testprint1 \(self.data.count)")
          loadNews() { response in
              self.data = response
  
              print("initprint \(self.data.count)")
              print(self.data["response"]["results"].count)
              print(self.data["response"]["results"][0]["id"].stringValue)
          }
          print("testprint2 \(self.data.count)")
      }
      func getNews() -&gt; JSON {        
          return data
      }
      func loadNews(completion: @escaping (JSON) -&gt; ()){
          Alamofire.request("...")
              .responseJSON{ response in
                  guard response.result.isSuccess,
                    let value = response.result.value else {
                      print("Error: \(String(describing: response.result.error))")
                      completion([])
                      return
                  }
                  let json = JSON(value)
                  completion(json)
          }     
      }
  }
  
  </code></pre>
  
  <p>issue that i'm facing is when i try to call the DataManager() instance in my ViewController, I'm not able to read data in the controller for some reason, here is the controller code (relevant one):</p>
  
  <pre><code>class SecondViewController: UITableViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          let data1 = DataManager.shared.getNews()
          print("qa \(data1.count)")
      }
  ...
  }
  </code></pre>
  
  <p>now what bothers me is - logic behind this should be simple,  <strong>let data1 = DataManager.shared.getNews()</strong>  - if i'm not wrong will (should) execute the following flow: </p>
  
  <p><strong>init()->loadNews()->getNews()</strong> 
  initialize method will call loadNews, loadNews will fetch data from API, fill the data array, and getNews is supposed to return the filled data array, but that flow doesn't seem correct then</p>
  
  <p><a href="https://i.stack.imgur.com/PSdZ6.png" rel="nofollow noreferrer">console output</a></p>
  
  <p>console output text</p>
  
  <pre><code>testprint1 0
  testprint2 0
  qa 0
  initprint 1
  50
  commentisfree/2019/dec/07/lost-my-faith-in-tech-evangelism-john-naughton
  </code></pre>
  
  <p>so it seems like both prints within init() get executed before loadNews() method that is between them, as well as "qa0" print that is printing the size of the array in the ViewController. </p>
  
  <p>now my question is, does anyone see a mistake here, is this happening because of long network query, or am I just missing something, because it seems to me that data is properly loaded and parsed, which is seen in last 2 lines of output, but i can't get it where i need it, like it dissapears. is my logic here wrong? if someone could help I would really appreciate it.</p>