1

I tried to save the data retrieved from Firebase to a global variable spendDict. However, after I called retrieveSpend(), spendDict was still empty outside the closure. How to save the final result from Firebase to a global variable in Swift. Thanks.

The function looks like this:

   var spendDict: [String:[String]] = [:]

   override func viewDidLoad() {
            super.viewDidLoad()


            retrieveSpend()


        }

   func retrieveSpend(){
        // the dict will be [time:[amount , amount , amount]]
         var spendDict :[String: [String]] = [:]

         let spendDB = Database.database().reference().child("Spend")
         spendDB.observe(.childAdded, with:{(snapshot) in
            let returnValue = snapshot.value as! Dictionary<String, String>
            var amount_retrieved = returnValue["amount"]
            var time_retrieved = returnValue["time"]
            var tag_retrieved = returnValue["tag"]

            if self.spendDict.index(forKey: time_retrieved!) == nil {
                // the key does not exist in the dictionary

                self.spendDict[time_retrieved!] = [amount_retrieved!]
            }
            else{


            self.spendDict[time_retrieved!]?.append(amount_retrieved!)
            }
            print("spendDict in closure ", spendDict)

        })
        print("spendDict outside the closure " , spendDict)



    }

Here is the output:

spendDict outside the closure  [:]
spendDict in closure  ["02-07-2018": ["7"]]
spendDict in closure  ["02-07-2018": ["7"], "02-06-2018": ["6"]]
spendDict in closure  ["02-05-2018": ["5"], "02-07-2018": ["7"], "02-06-2018": ["6"]]
spendDict in closure  ["02-06-2018": ["6"], "02-08-2018": ["8"], "02-05-2018": ["5"], "02-07-2018": ["7"]]
spendDict in closure  ["02-06-2018": ["6"], "02-08-2018": ["8"], "02-05-2018": ["5"], "02-07-2018": ["7"], "02-09-2018": ["9"]]
spendDict in closure  ["02-01-2018": ["1"], "02-06-2018": ["6"], "02-08-2018": ["8"], "02-05-2018": ["5"], "02-07-2018": ["7"], "02-09-2018": ["9"]]
spendDict in closure  ["02-09-2018": ["9"], "02-06-2018": ["6"], "02-02-2018": ["2"], "02-08-2018": ["8"], "02-01-2018": ["1"], "02-05-2018": ["5"], "02-07-2018": ["7"]]
spendDict in closure  ["02-09-2018": ["9"], "02-03-2018": ["3"], "02-06-2018": ["6"], "02-02-2018": ["2"], "02-08-2018": ["8"], "02-01-2018": ["1"], "02-05-2018": ["5"], "02-07-2018": ["7"]]
spendDict in closure  ["02-09-2018": ["9"], "02-03-2018": ["3"], "02-06-2018": ["6"], "02-02-2018": ["2"], "02-08-2018": ["8", "88"], "02-01-2018": ["1"], "02-05-2018": ["5"], "02-07-2018": ["7"]]
spendDict in closure  ["02-09-2018": ["9"], "02-03-2018": ["3"], "02-06-2018": ["6"], "02-02-2018": ["2"], "02-10-2018": ["10"], "02-08-2018": ["8", "88"], "02-01-2018": ["1"], "02-05-2018": ["5"], "02-07-2018": ["7"]]
FYY
  • 37
  • 6
  • 2
    Please look up "how to return value from *asynchronous* method" – this has been asked and answered repeatedly. – Martin R Feb 11 '18 at 17:22
  • I think value is returning before closure execution complete. – Sagar Chauhan Feb 11 '18 at 17:25
  • What do you expect. it's asynchronous. – Dominik Bucher Feb 11 '18 at 18:32
  • Thank you all for the help. I know I have to add `completionHandler` and `DispatchQueue.main.async()`, but I do not know where I should add them. Can someone please help me? – FYY Feb 11 '18 at 19:26
  • Could someone please help me modify this code? I have been trying different methods, but none of them worked. I just want to save the complete result from Firebase to a global variable and use the global variable to do other things. Thanks. – FYY Feb 11 '18 at 23:13

0 Answers0