title: Firebase Realtime Database in Swift
body: <p>After several hours of trying to figure out what's happening without finding an answer to fill my void anywhere, I finally decided to ask here.
  While I assume I do have a concurrency issue, I have no clue as to how to solve it...</p>
  <p>I have an application trying to pull data from a Firebase Realtime Database with the following content:</p>
  <pre><code>{
    &quot;products&quot; : {
      &quot;accessory&quot; : {
        &quot;foo1&quot; : {
          &quot;ean&quot; : 8793462789134,
          &quot;name&quot; : &quot;Foo 1&quot;
        },
        &quot;foo2&quot; : {
          &quot;ean&quot; : 8793462789135,
          &quot;name&quot; : &quot;Foo 2&quot;
        }          
      },
      &quot;cpu&quot; : {
        &quot;foo3&quot; : {
          &quot;ean&quot; : 8793462789115,
          &quot;name&quot; : &quot;Foo 3&quot;
        }
      },
      &quot;ios&quot; : {
        &quot;foo4&quot; : {
          &quot;ean&quot; : 8793462789120,
          &quot;name&quot; : &quot;Foo 4&quot;
        },
        &quot;foo5&quot; : {
          &quot;ean&quot; : 8793462789123,
          &quot;name&quot; : &quot;Foo 5&quot;
        }
      }
    }
  }
  </code></pre>
  <p>I have a data model in Product.swift:</p>
  <pre><code>class Product {
      var identifier: String
      var category: String
      var ean: Int
      var name: String
      
      init(identifier: String, category: String, ean: Int, name: String) {
          self.init(identifier: identifier)
          self.category = category
          self.ean = ean
          self.name = name
      }
  }
  </code></pre>
  <p>I want to fetch the data in another class called FirebaseFactory.swift. I plan to use to communicate with Firebase:</p>
  <pre><code>import Foundation
  import FirebaseDatabase
  
  class FirebaseFactory {
      
      var ref = Database.database().reference()
      
      func getAvailableProducts() -&gt; [Product] {
          var products = [Product]()
          var data: DataSnapshot = DataSnapshot()
         
          self.ref.child(&quot;products&quot;).queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in
              data = snapshot
              // 1st print statement
              print(&quot;From within closure: \(data)&quot;)
          }
          // 2nd print statement
          print(&quot;From outside the closure: \(data)&quot;)
          // Getting the products yet to be implemented...
          return products
          
      }
  }
  </code></pre>
  <p>For now, I am simply trying to call the <code>getAvailableProducts() -&gt; [Product]</code> function from one of my view controllers:</p>
  <pre><code>override func viewWillAppear(_ animated: Bool) {
      let products = FirebaseFactory().getAvailableProducts()
  }
  </code></pre>
  <p><strong>My Problem now is</strong> that the 2nd print is printed prior to the 1st – which also means that retrieving the data from the snapshot and assigning it to <code>data</code> variable does not take place. (I know that the code to create my Product objects is missing, but that part actually is not my issue – concurrency is...)</p>
  <p>Any hints – before I pull out any more of my hairs – is highly appreciated!!</p>