swift4从firebase检索所有数据然后返回

时间:2018-02-16 17:59:52

标签: swift firebase

我试图从firebase检索数据并使用completionHandler返回数据。但是,completionHandler仅从我的Firebase返回了部分数据。因此,我使用全局变量merged_spendDict来存储来自Firebase的数据。但是,merged_spendDict为空。

问题1:在completionHandler中返回之前,如何检索整个数据? 问题2:为什么更新后merged_spendDict为空?我知道检索数据是异步的。如何检索整个数据并将它们全部保存在全局变量中?

整个数据应如下所示:

["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"]]

我的输出是

merged_spendDict:  [:] // How to store data from Firebase to a global variable?
dictionary in ViewDidload ["02-07-2018": ["7"]]
spendDict in closure  ["02-07-2018": ["7"]]
dictionary in ViewDidload ["02-06-2018": ["6"]]
spendDict in closure  ["02-06-2018": ["6"]]
dictionary in ViewDidload ["02-05-2018": ["5"]]
spendDict in closure  ["02-05-2018": ["5"]]
dictionary in ViewDidload ["02-08-2018": ["8"]]
spendDict in closure  ["02-08-2018": ["8"]]
dictionary in ViewDidload ["02-09-2018": ["9"]]
spendDict in closure  ["02-09-2018": ["9"]]
dictionary in ViewDidload ["02-01-2018": ["1"]]
spendDict in closure  ["02-01-2018": ["1"]]
dictionary in ViewDidload ["02-02-2018": ["2"]]
spendDict in closure  ["02-02-2018": ["2"]]
dictionary in ViewDidload ["02-03-2018": ["3"]]
spendDict in closure  ["02-03-2018": ["3"]]
dictionary in ViewDidload ["02-08-2018": ["88"]]
spendDict in closure  ["02-08-2018": ["88"]]
dictionary in ViewDidload ["02-10-2018": ["10"]]
spendDict in closure  ["02-10-2018": ["10"]]

这是我的代码:

var merged_spendDict : [String:[String]] = [:]
    typealias spendClosure = (Dictionary<String,[String]>?) -> Void
    @IBOutlet weak var passMonth: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

//        retrieveSpend(completionHandler: spendClosure)

        retrieveSpend(){
            dict in

            self.merged_spendDict.update(other: dict)
            print("dictionary in ViewDidload", dict)

        }

        print("merged_spendDict: ",self.merged_spendDict)


    }

func retrieveSpend(completionHandler: @escaping ([String:[String]])->()){
        // 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
            spendDict.removeAll()
            let returnValue = snapshot.value as! Dictionary<String, String>
            var amount_retrieved = returnValue["amount"]
            var time_retrieved = returnValue["time"]
            var tag_retrieved = returnValue["tag"]

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

                spendDict[time_retrieved!] = [amount_retrieved!]
            }
            else{
                spendDict[time_retrieved!]?.append(amount_retrieved!)
            }

            completionHandler(spendDict)
            print("spendDict in closure ", spendDict)

        })



    }

请帮帮我。感谢。

0 个答案:

没有答案