任务排序

时间:2016-10-12 00:03:38

标签: ios swift

我希望完成任务,然后执行Segue并转到新页面。但由于某种原因,它没有执行Segue,它在“DOWNLOAD DONE”之前打印“我现在就在这里”。

class DownloadStocksVC: UIViewController {

    var stocks = [Stock]()

    override func viewDidLoad() {
        super.viewDidLoad()

        DataService.ds.REF_BASE.child("stocks").observeEventType(.Value, withBlock: { snapshot in
            print(snapshot.value)

            if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
                self.stocks = []
                for snap in snapshot {

                    if let stockDict = snap.value as? Dictionary<String, AnyObject> {
                        let symbol = snap.key
                        let stock = Stock(stockKey: symbol, dictionary: stockDict)
                        self.stocks.append(stock)

                    }
                }
            }
            print("DOWNLOAD DONE")
        })

        print("I AM HERE NOW")
        self.performSegueWithIdentifier("goFromDownloadToTable", sender: nil)

    }

1 个答案:

答案 0 :(得分:2)

您需要在下载完成后执行segue,而不是之前。

将您的通话移至performSegue至完成程序栏的末尾。您还需要在主队列上运行。

class DownloadStocksVC: UIViewController {
    var stocks = [Stock]()

    override func viewDidLoad() {
        super.viewDidLoad()
        DataService.ds.REF_BASE.child("stocks").observeEventType(.Value, withBlock: { snapshot in
            print(snapshot.value)

            if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
                self.stocks = []
                for snap in snapshot {
                    if let stockDict = snap.value as? Dictionary<String, AnyObject> {
                        let symbol = snap.key
                        let stock = Stock(stockKey: symbol, dictionary: stockDict)
                        self.stocks.append(stock)

                    }
                }
            }

            print("DOWNLOAD DONE")
            DispatchQueue.main.async {
                self.performSegueWithIdentifier("goFromDownloadToTable", sender: nil)
            }
        })
    }